循序渐进教程的第一步后出现错误

时间:2018-09-12 18:09:59

标签: python-3.x scrapy

I am following this tutorial。编写完第一个蜘蛛程序后,它指示我使用命令scrapy crawl quotes,但似乎出现错误。

这是我的代码:

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"

    def start_requests(self):
        urls = [
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        page = response.url.split("/")[-2]
        filename = 'quotes-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)

这是我遇到的错误:

 PS C:\Users\BB\desktop\scrapy\tutorial\spiders> scrapy crawl quotes

 2018-09-12 13:55:06 [scrapy.utils.log] INFO: Scrapy 1.5.0 started
 (bot: tutorial)

 2018-09-12 13:55:06 [scrapy.utils.log] INFO: Versions: lxml 4.2.1.0,
 libxml2 2.9.8, cssselect 1.0.3, parsel 1.4.0, w3lib 1.19.0, Twisted
 17.5.0, Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.0.2o 27 Mar
 2018), cryptography 2.2.2, Platform Windows-10-10.0.17134-SP0

 Traceback (most recent call last):

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\spiderloader.py",
 line 69, in load

 return self._spiders[spider_name]

 KeyError: 'quotes'


 During handling of the above exception, another exception occurred:


 Traceback (most recent call last):

 File "C:\Users\BB\Anaconda3\Scripts\scrapy-script.py", line 5, in
 <module>

 sys.exit(scrapy.cmdline.execute())

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\cmdline.py", line
 150, in execute

 _run_print_help(parser, _run_command, cmd, args, opts)

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\cmdline.py", line
 90, in _run_print_help

 func(*a, **kw)

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\cmdline.py", line
 157, in _run_command

 cmd.run(args, opts)

 File
 "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\commands\crawl.py",
 line 57, in run

 self.crawler_process.crawl(spname, **opts.spargs)

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\crawler.py", line
 170, in crawl

 crawler = self.create_crawler(crawler_or_spidercls)

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\crawler.py", line
 198, in create_crawler

 return self._create_crawler(crawler_or_spidercls)

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\crawler.py", line
 202, in _create_crawler

 spidercls = self.spider_loader.load(spidercls)

 File "C:\Users\BB\Anaconda3\lib\site-packages\scrapy\spiderloader.py",
 line 71, in load

 raise KeyError("Spider not found: {}".format(spider_name))

 KeyError: 'Spider not found: quotes'

1 个答案:

答案 0 :(得分:0)

好的,我已经创建了一个名为Spiders的文件夹,但是本教程已经为我完成了此工作,并且有一个_pycache? _在里面__?命令“抓取爬网引号”起作用所需的文件。简而言之,我是从错误的文件夹运行它的。