【问题标题】:Trying to get Scrapy into a project to run Crawl command试图让 Scrapy 进入项目以运行 Crawl 命令
【发布时间】:2011-02-14 02:18:03
【问题描述】:

我是 Python 和 Scrapy 的新手,我正在学习 Scrapy 教程。我已经能够通过使用 DOS 界面和输入来创建我的项目:

scrapy startproject dmoz

后面的教程参考爬取命令:

scrapy crawl dmoz.org

但每次我尝试运行时,我都会收到一条消息,指出这不是合法的命令。在进一步环顾四周时,我似乎需要进入一个项目,而这是我无法弄清楚的。我尝试将目录更改为我在 startproject 中创建的“dmoz”文件夹,但它根本无法识别 Scrapy。

我确定我遗漏了一些明显的东西,我希望有人能指出它。

【问题讨论】:

    标签: python scrapy web-crawler


    【解决方案1】:

    您必须在“startproject”文件夹中执行它。如果它找到您的 scrapy.cfg 文件,您将有另一个命令。你可以在这里看到区别:

    $ scrapy startproject bar
    $ cd bar/
    $ ls
    bar  scrapy.cfg
    $ scrapy
    Scrapy 0.12.0.2536 - project: bar
    
    Usage:
      scrapy <command> [options] [args]
    
    Available commands:
      crawl         Start crawling from a spider or URL
      deploy        Deploy project in Scrapyd target
      fetch         Fetch a URL using the Scrapy downloader
      genspider     Generate new spider using pre-defined templates
      list          List available spiders
      parse         Parse URL (using its spider) and print the results
      queue         Deprecated command. See Scrapyd documentation.
      runserver     Deprecated command. Use 'server' command instead
      runspider     Run a self-contained spider (without creating a project)
      server        Start Scrapyd server for this project
      settings      Get settings values
      shell         Interactive scraping console
      startproject  Create new project
      version       Print Scrapy version
      view          Open URL in browser, as seen by Scrapy
    
    Use "scrapy <command> -h" to see more info about a command
    
    
    $ cd ..
    $ scrapy
    Scrapy 0.12.0.2536 - no active project
    
    Usage:
      scrapy <command> [options] [args]
    
    Available commands:
      fetch         Fetch a URL using the Scrapy downloader
      runspider     Run a self-contained spider (without creating a project)
      settings      Get settings values
      shell         Interactive scraping console
      startproject  Create new project
      version       Print Scrapy version
      view          Open URL in browser, as seen by Scrapy
    
    Use "scrapy <command> -h" to see more info about a command
    

    【讨论】:

    • 谢谢安德斯。这就是我认为我应该做的事情,我很高兴我没有发疯。我在 DOS 中,当我更改目录并运行“dir”时,我会看到我的项目文件夹和 scrapy.cfg 文件。但是,当我运行 scrapy 命令时,我得到一个响应“scrapy 不被识别为内部或外部命令、可运行程序或批处理文件”
    • 另外,这里是 scrapy.cfg 文件的内容,以防它生成不正确 # Automatically created by: scrapy startproject # # 有关 [deploy] 部分的更多信息,请参阅:# doc.scrapy.org/topics/scrapyd.html [设置] 默认 = Test1.settings [部署] #url = localhost:6800 project = Test1
    • 所以如果“scrapy 未被识别”,它应该是 PATH 环境变量;如果您尝试使用完整路径执行它怎么样? (不知道在 Windows 中这些东西怎么样,抱歉:S)。 .cfg 似乎也很好。
    • 做到了!谢谢安德斯,迫不及待想深入了解它。
    • 如果你想避免 Scrapy 用它生成的文件和其他垃圾来接管你的项目的疯狂。使用 scrapy runspider foo.py 不幸的是,这意味着你不能做其他事情,比如提要导出器,但这是脑残开发人员的错。
    【解决方案2】:

    PATH 环境变量未设置。

    您可以通过找到系统属性(我的电脑 > 属性 > 高级系统设置)导航到高级选项卡并单击环境变量按钮来设置 Python 和 Scrapy 的 PATH 环境变量。在新窗口中,滚动到“系统变量”窗口中的“变量路径”并添加以下行,用分号分隔

    C:\{python 文件夹的路径} C:\{python 文件夹的路径}\Scripts

    例子

    C:\Python27;C:\Python27\Scripts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2020-01-25
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多