在PyPy上运行Scrapy

3

是否可以在 PyPy 上运行 Scrapy?我查看了文档和 GitHub 项目,但是唯一提到 PyPy 的地方是两年前有一些单元测试在 PyPy 上执行,参见PyPy support。还有Scrapy fails in PyPy的长时间讨论,发生在3年前,没有具体的解决方案或后续。

据我所知,Scrapy的主要依赖项Twisted已经被确认在PyPy上工作。Scrapy还使用lxml进行HTML解析,它有一个PyPy-友好的分支。另一个依赖项pyOpenSSL也得到了完全支持(感谢@Glyph的评论)。

1
pyOpenSSL得到了完全支持:https://travis-ci.org/pyca/pyopenssl/jobs/66236395 - Glyph
1
只是试一下。没有必要像这样问一个问题。 - hellow
1
@cookiesoft 首先,根据我所做的研究,这是一个在文档或其他地方都没有涉及的问题。此外,潜在地,它可能会对Scrapy的性能产生积极影响。最重要的是,我希望这个主题能够帮助其他有类似问题的人,或者那些试图找到改进其编写的网络爬虫代码速度的方法的人。请将鼠标悬停在踩按钮上,再次检查是否符合描述。 - alecxe
1个回答

5

可以,:-)

稍微详细一点,我已经在我的电脑上安装了一个带pip的pypy 2.6.0版本。只需运行pip install scrapy就几乎可以了。结果我需要为lxml安装一些额外的库。之后就没问题了。

安装完成后,我可以运行dmoz教程。例如:

[user@localhost scrapy_proj]# scrapy crawl dmoz
2015-06-30 14:34:45 [scrapy] INFO: Scrapy 1.0.0 started (bot: scrapy_proj)
2015-06-30 14:34:45 [scrapy] INFO: Optional features available: ssl, http11
2015-06-30 14:34:45 [scrapy] INFO: Overridden settings: {'BOT_NAME': 'scrapy_proj', 'NEWSPIDER_MODULE': 'scrapy_proj.spiders', 'SPIDER_MODULES': ['scrapy_proj.spiders']}
2015-06-30 14:34:45 [py.warnings] WARNING: :0: UserWarning: You do not have a working installation of the service_identity module: 'No module named service_identity'.  Please install it from <https://pypi.python.org/pypi/service_identity> and make sure all of its dependencies are satisfied.  Without the service_identity module and a recent enough pyOpenSSL to support it, Twisted can perform only rudimentary TLS client hostname verification.  Many valid certificate/hostname mappings may be rejected.

2015-06-30 14:34:45 [scrapy] INFO: Enabled extensions: CoreStats, TelnetConsole, CloseSpider, LogStats, SpiderState
2015-06-30 14:34:45 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-06-30 14:34:45 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-06-30 14:34:45 [scrapy] INFO: Enabled item pipelines: 
2015-06-30 14:34:45 [scrapy] INFO: Spider opened
2015-06-30 14:34:45 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-06-30 14:34:45 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-06-30 14:34:46 [scrapy] DEBUG: Crawled (200) <GET http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/> (referer: None)
2015-06-30 14:34:46 [scrapy] DEBUG: Crawled (200) <GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> (referer: None)
2015-06-30 14:34:46 [scrapy] INFO: Closing spider (finished)
2015-06-30 14:34:46 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 514,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 16286,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2015, 6, 30, 13, 34, 46, 219002),
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'log_count/WARNING': 1,
 'response_received_count': 2,
 'scheduler/dequeued': 2,
 'scheduler/dequeued/memory': 2,
 'scheduler/enqueued': 2,
 'scheduler/enqueued/memory': 2,
 'start_time': datetime.datetime(2015, 6, 30, 13, 34, 45, 652421)}
2015-06-30 14:34:46 [scrapy] INFO: Spider closed (finished)

根据您的要求,这里是有关我正在运行的版本的更多信息:

[user@localhost scrapy_proj]# which scrapy
/opt/pypy/bin/scrapy
[user@localhost scrapy_proj]# scrapy version
2015-06-30 15:04:42 [scrapy] INFO: Scrapy 1.0.0 started (bot: scrapy_proj)
2015-06-30 15:04:42 [scrapy] INFO: Optional features available: ssl, http11
2015-06-30 15:04:42 [scrapy] INFO: Overridden settings: {'BOT_NAME': 'scrapy_proj', 'NEWSPIDER_MODULE': 'scrapy_proj.spiders', 'SPIDER_MODULES': ['scrapy_proj.spiders']}
Scrapy 1.0.0

谢谢你的回答!你能再检查一下scrapy指向的位置吗?请运行which scrapy并发布你得到的输出。 - alecxe
Sorry, I cannot see any English text to translate. Please provide the content that needs to be translated. - Peter Brittain
很好!我成功地让ScrapyPyPy一起工作了。目前它可以正常运行。我将尝试在不同的爬虫和scrapyjs以及selenium结合使用时运行它,并且希望能够在这个主题中报告结果。谢谢,我将在一个小时内授予奖励。 - alecxe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接