python - scrapy规则不调用解析方法

标签 python scrapy

我是 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/

相关文章:

python - 单击 Scrapy-Splash 中的按钮

python - 使用 Xpath 使用多个条件从 <head> 中的元标记中提取内容

python - 我如何告诉 Scrapy 只抓取 Xpath 中的链接?

python - Pandas 将文本文件读入数据框

python - 如何查找一个列表是否按顺序是另一个列表的子集?

python - Matplotlib 有时会向图表添加比应有的更多绘图

Python 脚本仅在缓冲区使用套接字接收数据并发送 KeyboardInterrupt 后才中断

python - 系列数据不断增加

python - 从 HTML 文件顶部抓取 'dictionary' 类型对象(一堆文本,不在类中)

python - Scrapy crawl命令输出如何保存