【问题标题】:How to run Scrapy project in Jupyter?如何在 Jupyter 中运行 Scrapy 项目?
【发布时间】:2016-11-29 02:16:21
【问题描述】:

在 Mac 上,我安装了 Jupyter,当我从 Scrapy 项目的根文件夹中键入 jupyter notebook 时,它会打开笔记本。此时我可以浏览所有项目文件。

如何从笔记本执行项目?

如果我单击终端下的运行选项卡,我会看到:

There are no terminals running.

【问题讨论】:

    标签: python scrapy jupyter


    【解决方案1】:

    有两种主要方法可以实现这一目标:

    1。 在 Files 选项卡下打开一个新终端:New > Terminal
    然后简单地运行你的蜘蛛:scrapy crawl [options] <spider>

    2。 创建一个新笔记本并使用CrawlerProcessCrawlerRunner 类在单元格中运行:

    from scrapy.crawler import CrawlerProcess
    from scrapy.utils.project import get_project_settings
    
    process = CrawlerProcess(get_project_settings())
    
    process.crawl('your-spider')
    process.start() # the script will block here until the crawling is finished
    

    Scrapy docs - Run Scrapy from a script

    【讨论】:

      【解决方案2】:

      无需终端即可运行 Spyder 类。只需在 jupyter-notebook 单元格中添加以下代码:

      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
      

      欲了解更多信息,请参阅here

      【讨论】:

        【解决方案3】:

        Jupyter 具有从单元格本身运行命令行参数的快捷方式。以! 开始单元格,然后像往常一样在控制台中键入命令的其余部分。

        Read More

        【讨论】:

          猜你喜欢
          • 2022-01-11
          • 2015-03-21
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 2017-07-03
          • 2018-10-30
          • 2022-07-05
          相关资源
          最近更新 更多