栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

CrawlerProcess与CrawlerRunner

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CrawlerProcess与CrawlerRunner

Scrapy的文档在给出两者的实际应用示例方面做得非常糟糕。

CrawlerProcess
假设scrapy是唯一使用twisted反应堆的东西。如果你在python中使用线程来运行其他代码,则并非总是如此。让我们以此为例。

from scrapy.crawler import CrawlerProcessimport scrapydef notThreadSafe(x):    """do something that isn't thread-safe"""    # ...class MySpider1(scrapy.Spider):    # Your first spider definition    ...class MySpider2(scrapy.Spider):    # Your second spider definition    ...process = CrawlerProcess()process.crawl(MySpider1)process.crawl(MySpider2)process.start() # the script will block here until all crawling jobs are finishednotThreadSafe(3) # it will get executed when the crawlers stop

现在,你可以看到,该函数仅在搜寻器停止时才执行,如果我希望在搜寻器在同一反应堆中爬行时执行该函数,该怎么办?

from twisted.internet import reactorfrom scrapy.crawler import CrawlerRunnerimport scrapydef notThreadSafe(x):    """do something that isn't thread-safe"""    # ...class MySpider1(scrapy.Spider):    # Your first spider definition    ...class MySpider2(scrapy.Spider):    # Your second spider definition    ...runner = CrawlerRunner()runner.crawl(MySpider1)runner.crawl(MySpider2)d = runner.join()d.addBoth(lambda _: reactor.stop())reactor.callFromThread(notThreadSafe, 3)reactor.run() #it will run both crawlers and pre inside the function

Runner类不限于此功能



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368491.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号