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