gpt4 book ai didi

python - 定义 LxmlLinkExtractor 规则时如何使用 RegEx 列表

转载 作者:行者123 更新时间:2023-12-01 03:52:41 24 4
gpt4 key购买 nike

我想知道如何在 Scrapy 蜘蛛之外定义 RegEx 列表,然后将 RegEx 读入 LxmlLinkExtractor

我正在使用当前代码:

file = open("myFile.txt")
regexs = [rule.strip() for rule in file.readlines()]
file.close()
return regexs

然后将返回值作为参数传递,如下所示:

Rule(LinkExtractor(allow=(regexs, )), callback='parse_file')

这会导致以下错误:

TypeError: unhashable type: 'list' 

最佳答案

应该有效:

regexs = [rule.strip() for rule in file.readlines()]
LinkExtractor(allow=regexs, callback='parse_file')

在此处查看有关允许参数的更多信息: http://doc.scrapy.org/en/latest/topics/link-extractors.html#module-scrapy.linkextractors.lxmlhtml

关于python - 定义 LxmlLinkExtractor 规则时如何使用 RegEx 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37994874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com