gpt4 book ai didi

python - 无法运行 'scrapy crawl quotes'

转载 作者:行者123 更新时间:2023-12-01 01:16:06 24 4
gpt4 key购买 nike

无法让 scrapy 教程工作。

我正在尝试学习 scrapy,但甚至无法运行教程。我尝试在 python 3.7 和 3.5.5 中运行它,得到相同的结果

导入scrapy

类 QuotesSpider(scrapy.Spider): 名称 =“引号”

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)

这似乎运行正常。至少它不会抛出任何错误。

当我在 Anaconda 提示窗口中运行“scrapycrapyquotes”时,我得到:

"hed) C:\Users\userOne\python script files\scrapy\tutorial>scrapy crawl 
quotes
2019-01-23 18:34:27 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot:
tutorial)
2019-01-23 18:34:27 [scrapy.utils.log] INFO: Versions: lxml 4.2.3.0, libxml2
2.9.5, cssselect 1.0.3, parsel 1.5.0, w3lib 1.19.0, Twisted 18.7.0, Python
3.5.5 | packaged by conda-forge | (default, Jul 24 2018, 01:52:17) [MSC
v.1900 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.0.2p 14 Aug 2018),
cryptography 2.3.1, Platform Windows-10-10.0.17134-SP0
Traceback (most recent call last):
File "C:\Users\userOne\Anaconda3\envs\hed\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\userOne\Anaconda3\envs\hed\Scripts\scrapy-script.py", line
10, in <module>
sys.exit(execute())
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\cmdline.py", line 150, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\cmdline.py", line 90, in _run_print_help
func(*a, **kw)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\cmdline.py", line 157, in _run_command
cmd.run(args, opts)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\commands\crawl.py", line 57, in run
self.crawler_process.crawl(spname, **opts.spargs)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\crawler.py", line 170, in crawl
crawler = self.create_crawler(crawler_or_spidercls)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\crawler.py", line 198, in create_crawler
return self._create_crawler(crawler_or_spidercls)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\crawler.py", line 202, in _create_crawler
spidercls = self.spider_loader.load(spidercls)
File "C:\Users\userOne\Anaconda3\envs\hed\lib\site- packages\scrapy\spiderloader.py", line 71, in load
raise KeyError("Spider not found: {}".format(spider_name))
KeyError: 'Spider not found: quotes'

"

输出应与此类似:

"016-12-16 21:24:05 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-12-16 21:24:05 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-12-16 21:24:05 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://quotes.toscrape.com/robots.txt> (referer: None)
2016-12-16 21:24:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None)
2016-12-16 21:24:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/2/> (referer: None)
2016-12-16 21:24:05 [quotes] DEBUG: Saved file quotes-1.html
2016-12-16 21:24:05 [quotes] DEBUG: Saved file quotes-2.html
2016-12-16 21:24:05 [scrapy.core.engine] INFO: Closing spider (finished)"

预先感谢您提供的任何帮助。

最佳答案

也许您的源代码放置在错误的目录中?

我遇到了一个非常相似(如果不是相同)的问题。 (我没有使用 Anaconda,但错误也是“第 69 行,加载返回 self._spiders[spider_name] KeyError: 'quotes'”。

对我来说解决这个问题的方法是将源代码文件(quotes_spider.py)从projectname/tutorial/tutorial目录移动到projectname/tutorial/tutorial/spiders目录。

来自教程页面。 。 。“这是我们第一个 Spider 的代码。将其保存在项目的tutorial/spiders 目录下名为quotes_spider.py 的文件中”

关于python - 无法运行 'scrapy crawl quotes',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54337540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com