サイト全体をクロールし、条件付きでリンクを抽出したい。
このリンクで提案されているように、複数のルールを試しましたが、機能しません。Scrapy はすべてのページをクロールするわけではありません
このコードを試してみましたが、詳細は破棄されません。
class BusinesslistSpider(CrawlSpider):
name = 'businesslist'
allowed_domains = ['www.businesslist.ae']
start_urls = ['http://www.businesslist.ae/']
rules = (
Rule(SgmlLinkExtractor()),
Rule(SgmlLinkExtractor(allow=r'company/(\d)+/'), callback='parse_item'),
)
def parse_item(self, response):
self.log('Hi, this is an item page! %s' % response.url)
hxs = HtmlXPathSelector(response)
i = BusinesslistItem()
company = hxs.select('//div[@class="text companyname"]/strong/text()').extract()[0]
address = hxs.select('//div[@class="text location"]/text()').extract()[0]
location = hxs.select('//div[@class="text location"]/a/text()').extract()[0]
i['url'] = response.url
i['company'] = company
i['address'] = address
i['location'] = location
return i
私の場合、2 番目のルールは適用されないため、詳細ページは解析されません。