从Python身上跑出来

2024-04-25 00:19:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着从Python上运行Scrapy。我正在查看以下代码(source):

from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy import log
from testspiders.spiders.followall import FollowAllSpider

spider = FollowAllSpider(domain='scrapinghub.com')
crawler = Crawler(Settings())
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run() # the script will block here

我的问题是,我不明白如何调整这段代码来运行我自己的spider。我把我的spider项目称为“spider_a”,它指定在spider中爬行的域。在

我要问的是,如果我用以下代码运行我的蜘蛛:

^{pr2}$

如何调整上面的示例python代码以实现同样的效果?在


Tags: 代码fromimportlogsourcesettingstwistedstart
2条回答

只需导入它并传递到crawler.crawl(),如:

from testspiders.spiders.spider_a import MySpider

spider = MySpider()
crawler.crawl(spider)

在scrapy0.19.x中(可能与旧版本一起工作),您可以执行以下操作。在

spider = FollowAllSpider(domain='scrapinghub.com')
settings = get_project_settings()
crawler = Crawler(settings)
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run() # the script will block here

您甚至可以直接从以下脚本调用命令:

^{pr2}$

看看我的答案here。我changed官方的documentation所以现在你的爬虫程序使用你的设置并可以产生输出。在

相关问题 更多 >