【问题标题】:Scrapy: LinkExtractor not workingScrapy:LinkExtractor 不工作
【发布时间】:2015-11-06 22:09:47
【问题描述】:

我正在尝试抓取 Erowid 并收集有关体验的数据。我试图从关于药物的一般信息到实际体验本身。

但是 LinkExtractor 似乎无法正常工作。

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import HtmlXPathSelector

from Erowid.items import ErowidItem


class ExperiencesSpider(CrawlSpider):
    name = "test"
    allowed_domains = ["www.erowid.org"]
    start_urls = ['https://www.erowid.org/experiences/subs/exp_aPVP.shtml']
    rules = [ 
        Rule(LinkExtractor(allow =('/experiences/exp.php?ID=[0-9]+')),     callback = 'parse_item', follow = True)

    ]
    def parse_item(self, response):
        [other code]

来自https://www.erowid.org/experiences/subs/exp_aPVP.shtml,我正在尝试获得href为

的体验
/experiences/exp.php?ID=  (some digits)

我在 ID 后找不到正确的代码,我已经尝试过各种不同的正则表达式,包括

\d+ and [0-9]+

错误是由不正确的正则表达式引起的吗?如果是,那么正确的正则表达式是什么?如果不是,那么为什么会发生此错误,我该如何解决?

【问题讨论】:

    标签: python regex web-scraping scrapy scrapy-spider


    【解决方案1】:

    这是适合我的表达方式:

    /experiences/exp\.php\?ID=\d+$
    

    这是rules 的外观:

    rules = [
        Rule(LinkExtractor(allow=r'/experiences/exp\.php\?ID=\d+$'),
             callback='parse_item', follow=True)
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2018-12-22
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多