[英]Issues on following links in scrapy
我想抓取一個包含幾個類別的網站的博客。 從第一個類別開始瀏覽頁面,我的目標是按照類別收集每個網頁。 我從第一類收集網站,但是蜘蛛停在這里,無法進入第二類。
草稿示例:
我的代碼:
import scrapy
from scrapy.contrib.spiders import Rule, CrawlSpider
from scrapy.contrib.linkextractors import LinkExtractor
from final.items import DmozItem
class my_spider(CrawlSpider):
name = 'heart'
allowed_domains = ['greek-sites.gr']
start_urls = ['http://www.greek-sites.gr/categories/istoselides-athlitismos']
rules = (Rule(LinkExtractor(allow=(r'.*categories/.*', )), callback='parse', follow=True),)
def parse(self, response):
self.logger.info('Hi, this is an item page! %s', response.url)
categories = response.xpath('//a[contains(@href, "categories")]/text()').extract()
for category in categories:
item = DmozItem()
item['title'] = response.xpath('//a[contains(text(),"gr")]/text()').extract()
item['category'] = response.xpath('//div/strong/text()').extract()
return item
問題很簡單: callback
必須不同於parse
,因此我建議您以方法parse_site
為例,然后准備繼續進行抓取。
如果您在下面進行更改,它將起作用:
rules = (Rule(LinkExtractor(allow=(r'.*categories/.*', )), callback='parse_site', follow=True),)
def parse_site(self, response):
其原因在docs中進行了描述:
編寫爬網蜘蛛規則時,請避免將
parse
用作回調,因為CrawlSpider
使用parse
方法本身來實現其邏輯。 因此,如果您覆蓋parse
方法,則爬網蜘蛛將不再起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.