python - Scrapy:LinkExtractor 不工作

标签 python regex web-scraping scrapy

我正在尝试抓取 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]+

该错误是由不正确的正则表达式引起的吗?如果是,那么正确的正则表达式是什么?如果不是,那么为什么会出现此错误以及如何修复它?

最佳答案

这是适合我的表达方式:

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

以下是规则的外观:

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

关于python - Scrapy:LinkExtractor 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31996741/

相关文章:

python - 正则表达式 : How to access multiple matches of a group?

c# - .NET正则表达式识别 `if .. then .. else .. endif`

Python Pandas - reshape 数据框

python - 有没有办法为 Jupyter Notebook 创建桌面快捷方式?

Python字符串到颜色

html - 如何提取向下滚动时进一步加载的完整表格?

python - selenium 不能在 url 中使用 &

Python 脚本对目录中的所有文件运行 exe 文件

r - 如何通过R的逆模式提取子串?

python - 从 html <script> 中提取 JSON 对象