How to use PyCharm to debug Scrapy projects

后端 未结 10 1401
长发绾君心
长发绾君心 2020-12-02 04:00

I am working on Scrapy 0.20 with Python 2.7. I found PyCharm has a good Python debugger. I want to test my Scrapy spiders using it. Anyone knows how to do that please?

<
相关标签:
10条回答
  • 2020-12-02 04:39

    I am running scrapy in a virtualenv with Python 3.5.0 and setting the "script" parameter to /path_to_project_env/env/bin/scrapy solved the issue for me.

    0 讨论(0)
  • 2020-12-02 04:40

    You just need to do this.

    Create a Python file on crawler folder on your project. I used main.py.

    • Project
      • Crawler
        • Crawler
          • Spiders
          • ...
        • main.py
        • scrapy.cfg

    Inside your main.py put this code below.

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

    And you need to create a "Run Configuration" to run your main.py.

    Doing this, if you put a breakpoint at your code it will stop there.

    0 讨论(0)
  • 2020-12-02 04:40

    I use this simple script:

    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()
    
    0 讨论(0)
  • 2020-12-02 04:41

    As of 2018.1 this became a lot easier. You can now select Module name in your project's Run/Debug Configuration. Set this to scrapy.cmdline and the Working directory to the root dir of the scrapy project (the one with settings.py in it).

    Like so:

    Now you can add breakpoints to debug your code.

    0 讨论(0)
提交回复
热议问题