我正在使用 Python 2.7 开发 Scrapy 0.20。我发现 PyCharm 有一个很好的 Python 调试器。我想用它来测试我的 Scrapy 蜘蛛。请问有人知道该怎么做吗?
我试过的
File->Setting->Project structure->Add content root.
但我不知道我还需要做什么
scrapy
命令是一个 Python 脚本,这意味着您可以从 PyCharm 内部启动它。
当您检查 scrapy 二进制文件 (which scrapy
) 时,您会注意到这实际上是一个 python 脚本:
#!/usr/bin/python
from scrapy.cmdline import execute
execute()
这意味着像 scrapy crawl IcecatCrawler
这样的命令也可以像这样执行:python /Library/Python/2.7/site-packages/scrapy/cmdline.py crawl IcecatCrawler
尝试找到 scrapy.cmdline 包。就我而言,位置在这里:/Library/Python/2.7/site-packages/scrapy/cmdline.py
使用该脚本作为脚本在 PyCharm 中创建运行/调试配置。使用scrapy 命令和spider 填充脚本参数。在这种情况下 crawl IcecatCrawler
。
https://i.stack.imgur.com/bcDQH.png
将断点放在爬取代码中的任何位置,它应该可以工作™。
你只需要这样做。
在项目的爬虫文件夹中创建一个 Python 文件。我使用了 main.py。
项目爬行者爬行者蜘蛛... main.py scrapy.cfg
爬行者爬行者蜘蛛 ... main.py scrapy.cfg
爬行蜘蛛...
蜘蛛
...
主文件
scrapy.cfg
在您的 main.py 中,将此代码放在下面。
from scrapy import cmdline
cmdline.execute("scrapy crawl spider".split())
你需要创建一个“运行配置”来运行你的 main.py。
这样做,如果你在你的代码中放置一个断点,它就会停在那里。
从 2018.1 开始,这变得容易多了。您现在可以在项目的 Run/Debug Configuration
中选择 Module name
。将此设置为 scrapy.cmdline
并将 Working directory
设置为 scrapy 项目的根目录(其中包含 settings.py
的那个)。
像这样:
https://i.stack.imgur.com/mAOiY.png
现在您可以添加断点来调试您的代码。
我正在使用 Python 3.5.0 在 virtualenv 中运行 scrapy,并将“脚本”参数设置为 /path_to_project_env/env/bin/scrapy
为我解决了这个问题。
project/crawler/crawler
,即保存 __init__.py
的目录。
intellij 想法也有效。
创建 main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#coding=utf-8
import sys
from scrapy import cmdline
def main(name):
if name:
cmdline.execute(name.split())
if __name__ == '__main__':
print('[*] beginning main thread')
name = "scrapy crawl stack"
#name = "scrapy crawl spa"
main(name)
print('[*] main thread exited')
print('main stop====================================================')
显示如下:
https://i.stack.imgur.com/5ZMdV.png
https://i.stack.imgur.com/5M4IR.png
https://i.stack.imgur.com/iOUvK.png
为了给已接受的答案添加一点内容,将近一个小时后,我发现我必须从下拉列表(靠近图标工具栏的中心)中选择正确的运行配置,然后单击“调试”按钮才能使其工作。希望这可以帮助!
根据文档https://doc.scrapy.org/en/latest/topics/practices.html
import scrapy
from scrapy.crawler import CrawlerProcess
class MySpider(scrapy.Spider):
# Your spider definition
...
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(MySpider)
process.start() # the script will block here until the crawling is finished
我也在使用 PyCharm,但我没有使用它的内置调试功能。
对于调试,我使用 ipdb
。我设置了一个键盘快捷键以在我希望断点发生的任何行上插入 import ipdb; ipdb.set_trace()
。
然后我可以输入 n
执行下一条语句,s
进入函数,输入任何对象名称以查看其值,更改执行环境,输入 c
继续执行...
这非常灵活,可以在 PyCharm 以外的环境中工作,在这些环境中您不控制执行环境。
只需输入您的虚拟环境 pip install ipdb
并将 import ipdb; ipdb.set_trace()
放在您希望暂停执行的行上。
更新
您也可以 pip install pdbpp
并使用标准 import pdb; pdb.set_trace
而不是 ipdb
。我认为 PDB++ 更好。
我使用这个简单的脚本:
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
process = CrawlerProcess(get_project_settings())
process.crawl('your_spider_name')
process.start()
runner.py
。这很重要的原因是它有意加载项目设置文件。如果您尝试加载管道,则必须这样做。
扩展@Rodrigo 的答案版本我添加了这个脚本,现在我可以从配置中设置蜘蛛名称,而不是更改字符串。
import sys
from scrapy import cmdline
cmdline.execute(f"scrapy crawl {sys.argv[1]}".split())
不定期副业成功案例分享
ImportError: No module named settings
我检查了工作目录是项目目录。它在 Django 项目中使用。还有其他人偶然发现了这个问题吗?Working directory
,否则会报错no active project, Unknown command: crawl, Use "scrapy" to see available commands, Process finished with exit code 2