gpt4 book ai didi

python - 创建一个通用的 scrapy 蜘蛛

转载 作者:太空狗 更新时间:2023-10-29 16:58:10 28 4
gpt4 key购买 nike

我的问题实际上是如何做与上一个问题相同的事情,但在 Scrapy 0.14 中。

Using one Scrapy spider for several websites

基本上,我有一个 GUI,它接受域、关键字、标签名称等参数,我想创建一个通用蜘蛛程序来为那些标签中的那些关键字爬行这些域。我读过相互矛盾的东西,使用旧版本的 scrapy,通过覆盖蜘蛛管理器类或动态创建蜘蛛。首选哪种方法以及如何实现和调用正确的解决方案?提前致谢。

这是我想要通用的代码。它还使用 BeautifulSoup。我将它配对,希望没有删除任何对理解它至关重要的东西。

class MySpider(CrawlSpider):

name = 'MySpider'
allowed_domains = ['somedomain.com', 'sub.somedomain.com']
start_urls = ['http://www.somedomain.com']

rules = (
Rule(SgmlLinkExtractor(allow=('/pages/', ), deny=('', ))),

Rule(SgmlLinkExtractor(allow=('/2012/03/')), callback='parse_item'),
)

def parse_item(self, response):
contentTags = []

soup = BeautifulSoup(response.body)

contentTags = soup.findAll('p', itemprop="myProp")

for contentTag in contentTags:
matchedResult = re.search('Keyword1|Keyword2', contentTag.text)
if matchedResult:
print('URL Found: ' + response.url)

pass

最佳答案

您可以创建一个由解释器评估的运行时蜘蛛。这段代码可以在运行时像这样评估:

a = open("test.py")
from compiler import compile
d = compile(a.read(), 'spider.py', 'exec')
eval(d)

MySpider
<class '__main__.MySpider'>
print MySpider.start_urls
['http://www.somedomain.com']

关于python - 创建一个通用的 scrapy 蜘蛛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814827/

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