首页 文章

如何使用PyCharm来调试Scrapy项目

提问于
浏览
81

我正在使用Python 2.7开发Scrapy 0.20 . 我发现PyCharm有一个很好的Python调试器 . 我想用它来测试我的Scrapy蜘蛛 . 有人知道怎么做吗?

我尝试了什么

实际上我试图将蜘蛛作为一个脚本运行 . 结果,我构建了这个脚本 . 然后,我尝试将我的Scrapy项目添加到PyCharm作为这样的模型:

File->Setting->Project structure->Add content root.

但我不知道还有什么需要做的

9 回答

  • 0

    截至2018.1,这变得容易多了 . 您现在可以在项目的 Run/Debug Configuration 中选择 Module name . 将此设置为 scrapy.cmdline ,将 Working directory 设置为scrapy项目的根目录(其中包含 settings.py 的目录) .

    像这样:

    PyCharm Scrapy debug configuration

    现在,您可以添加断点来调试代码 .

  • 4

    你只需要这样做 .

    在项目的crawler文件夹上创建一个Python文件 . 我用了main.py.

    • 项目

    • Crawler

    • Crawler

    • 蜘蛛

    • ......

    • main.py

    • scrapy.cfg

    你的main.py里面放了这个代码 .

    from scrapy import cmdline    
    cmdline.execute("scrapy crawl spider".split())
    

    你需要创建一个“运行配置”来运行你的main.py.

    这样做,如果你在你的代码上放一个断点,它就会停在那里 .

  • 3

    我在使用Python 3.5.0的virtualenv中运行scrapy并将"script"参数设置为 /path_to_project_env/env/bin/scrapy 为我解决了这个问题 .

  • 2

    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 .

    像这样:
    PyCharm Run/Debug Configuration

    将您的断点放在爬行代码中的任何位置,它应该可以工作 .

  • 8

    我也在使用PyCharm,但我没有使用它的内置调试功能 .

    为了调试我正在使用ipdb . 我设置了一个键盘快捷键,在我希望断点发生的任何一行上插入 import ipdb; ipdb.set_trace() .

    然后我可以输入 n 来执行下一个语句, s 进入函数,键入任何对象名称以查看其值,更改执行环境,输入 c 继续执行...

    这非常灵活,适用于PyCharm以外的环境,您无法控制执行环境 .

    只需输入您的虚拟环境 pip install ipdb 并将 import ipdb; ipdb.set_trace() 放在您希望执行暂停的行上 .

  • 0

    根据文档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
    
  • 87

    我用这个简单的脚本:

    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()
    
  • 145

    intellij idea 也有效 .

    创建 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====================================================')
    

    显示如下:

    enter image description here

    enter image description here

    enter image description here

  • 3

    为了给接受的答案添加一点,几乎一个小时后我发现我必须从下拉列表中选择正确的运行配置(靠近图标工具栏的中心),然后单击调试按钮以使其工作 . 希望这可以帮助!

相关问题