顺序抓取网站使用scrapy
问题描述:
有没有办法告诉scrapy停止根据第二级页面的条件抓取?我做了以下情况:顺序抓取网站使用scrapy
- 我有一个START_URL开始与(第一级页)
- 我一直在使用解析设置从START_URL提取的URL(个体经营, 响应)
- 然后,添加排队使用请求与回调为parseDetailPage(个体经营,响应)
- 在parseDetail(2级页)我来的链接,知道我是否可以停止爬行或不
现在我使用CloseSpider()来实现这一点,但问题是,当我开始爬取二级页面时,要解析的URL已经排队,我不知道如何从队列中移除它们。有没有办法顺序抓取链接列表,然后能够停在parseDetailPage?
global job_in_range
start_urls = []
start_urls.append("http://sfbay.craigslist.org/sof/")
def __init__(self):
self.job_in_range = True
def parse(self, response):
hxs = HtmlXPathSelector(response)
results = hxs.select('//blockquote[@id="toc_rows"]')
items = []
if results:
links = results.select('.//p[@class="row"]/a/@href')
for link in links:
if link is self.end_url:
break;
nextUrl = link.extract()
isValid = WPUtil.validateUrl(nextUrl);
if isValid:
item = WoodPeckerItem()
item['url'] = nextUrl
item = Request(nextUrl, meta={'item':item},callback=self.parseDetailPage)
items.append(item)
else:
self.error.log('Could not parse the document')
return items
def parseDetailPage(self, response):
if self.job_in_range is False:
raise CloseSpider('End date reached - No more crawling for ' + self.name)
hxs = HtmlXPathSelector(response)
print response
body = hxs.select('//article[@id="pagecontainer"]/section[@class="body"]')
item = response.meta['item']
item['postDate'] = body.select('.//section[@class="userbody"]/div[@class="postinginfos"]/p')[1].select('.//date/text()')[0].extract()
if item['jobTitle'] is 'Admin':
self.job_in_range = False
raise CloseSpider('Stop crawling')
item['jobTitle'] = body.select('.//h2[@class="postingtitle"]/text()')[0].extract()
item['description'] = body.select(str('.//section[@class="userbody"]/section[@id="postingbody"]')).extract()
return item
我想在parseDetail页面满足条件时完全停止爬网,而不是恢复它。我面临的问题是,队列中已经有大量的url,无论提升CloseSpider,scrapy都会抓取。 – Praveer 2013-02-25 20:18:15
您使用了哪种CloseSpider? scrapy.contrib.closespider.CloseSpider?或scrapy.exceptions.CloseSpider? – 2013-02-26 08:04:34