我是 scrapy 的新手,正在尝试抓取域名,跟踪所有内部链接并使用/example/模式抓取 url 的标题。*
爬行有效,但标题抓取无效,因为输出文件为空。很可能我弄错了规则。为了实现我正在寻找的目标,这是使用规则的正确语法吗?
import scrapy
class BidItem(scrapy.Item):
url = scrapy.Field()
title = scrapy.Field()
蜘蛛.py
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from bid.items import BidItem
class GetbidSpider(CrawlSpider):
name = 'getbid'
allowed_domains = ['domain.de']
start_urls = ['https://www.domain.de/']
rules = (
Rule(
LinkExtractor(),
follow=True
),
Rule(
LinkExtractor(allow=['example/.*']),
callback='parse_item'
),
)
def parse_item(self, response):
href = BidItem()
href['url'] = response.url
href['title'] = response.css("h1::text").extract()
return href
抓取:scrapy抓取getbid -o 012916.csv
最佳答案
来自CrawlSpider
docs :
If multiple rules match the same link, the first one will be used, according to the order they’re defined in this attribute.
由于您的第一条规则将匹配所有链接,因此将始终使用它,而所有其他规则将被忽略。
解决问题就像切换规则的顺序一样简单。
关于python - scrapy规则不调用解析方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53436914/