gpt4 book ai didi

找不到 Python scrapy spider KeyError

转载 作者:行者123 更新时间:2023-11-28 17:19:28 25 4
gpt4 key购买 nike

几天来,我一直在尝试在 Scrapy 中创建一个爬虫,但在每个项目中,我总是遇到同样的错误:spider not found。无论我做了什么更改或遵循哪个教程,它总是返回相同的错误。

有人可以建议我应该在哪里寻找错误吗?

谢谢!

window 10, python 2.7

C:.
│ scrapy.cfg

└───scrapscrapy
│ items.py
│ middlewares.py
│ pipelines.py
│ settings.py
│ settings.pyc
│ __init__.py
│ __init__.pyc

└───spiders
SSSpider.py
SSSpider.pyc

items.py

from scrapy.item import Item, Field

class ScrapscrapyItem(Item):
# define the fields for your item here like:
# name = scrapy.Field()
Heading = Field()
Content = Field()
Source_Website = Field()
pass

SSSpider.py

from scrapy.selector import Selector
from scrapy.spider import Spider
from Scrapscrapy.items import ScrapscrapyItem

class ScrapscrapySpider(Spider):
name="ss"
allowed_domains = ["yellowpages.md/rom/companies/info/2683-intelsmdv-srl"]
start_url = ['http://yellowpages.md/rom/companies/info/2683-intelsmdv-srl/']

def parse(self, response) :
sel = Selector (response)
item = ScrapscrapyItem()
item['Heading']=sel.xpath('/html/body/div[2]/div[2]/div/div/div/div/div[1]/div/div[2]/div/article/div/div[1]/div[2]/h2').extract
item['Content']=sel.xpath('/html/body/div[2]/div[2]/div/div/div/div/div[1]/div/div[2]/div/article/div/div[1]/div[2]/div[2]/div/div[2]/div/div[1]/div[1]').extract
item['Source_Website']= 'yellowpages.md/rom/companies/info/2683-intelsmdv-srl'
return item

设置

BOT_NAME = 'scrapscrapy'

SPIDER_MODULES = ['scrapscrapy.spiders']
NEWSPIDER_MODULE = 'scrapscrapy.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'scrapscrapy (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

命令行:

C:\Users\nastea\Desktop\scrapscrapy>scrapy crawl ss
c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\spiderloader.py:37: RuntimeWarning:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\spiderloader.py", line 31, in _load_all_spiders
for module in walk_modules(name):
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\utils\misc.py", line 63, in walk_modules
mod = import_module(path)
File "c:\python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named spiders
Could not load spiders from module 'scrapscrapy.spiders'. Check SPIDER_MODULES setting
warnings.warn(msg, RuntimeWarning)
2017-02-19 14:21:16 [scrapy.utils.log] INFO: Scrapy 1.3.2 started (bot: scrapscrapy)
2017-02-19 14:21:16 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'scrapscrapy.spiders', 'SPIDER_MODULES': ['scrapscrapy.spiders'], 'ROBOTSTXT_OBEY': True, 'BOT_NAME': 'scrapscrapy'}
Traceback (most recent call last):
File "c:\python27\Scripts\scrapy-script.py", line 11, in <module>
load_entry_point('scrapy==1.3.2', 'console_scripts', 'scrapy')()
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\cmdline.py", line 142, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\cmdline.py", line 88, in _run_print_help
func(*a, **kw)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\cmdline.py", line 149, in _run_command
cmd.run(args, opts)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\commands\crawl.py", line 57, in run
self.crawler_process.crawl(spname, **opts.spargs)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\crawler.py", line 162, in crawl
crawler = self.create_crawler(crawler_or_spidercls)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\crawler.py", line 190, in create_crawler
return self._create_crawler(crawler_or_spidercls)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\crawler.py", line 194, in _create_crawler
spidercls = self.spider_loader.load(spidercls)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\spiderloader.py", line 51, in load
raise KeyError("Spider not found: {}".format(spider_name))
KeyError: 'Spider not found: ss'

编辑

正如 eLRuLL 所建议的,我将 _init_.py 文件添加到 spider 文件夹中,还将 scrapy.spider 更改为 scrapy.spiders,因为它告诉我它已被删除。现在 cmd 返回的结果是这样的:

C:\Users\nastea\Desktop\scrapscrapy>scrapy crawl ss
c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\spiderloader.py:37: RuntimeWarning:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\spiderloader.py", line 31, in _load_all_spiders
for module in walk_modules(name):
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\utils\misc.py", line 71, in walk_modules
submod = import_module(fullpath)
File "c:\python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\nastea\Desktop\scrapscrapy\scrapscrapy\spiders\SSSpider.py", line 3, in <module>
from Scrapscrapy.items import ScrapscrapyItem
ImportError: No module named Scrapscrapy.items
Could not load spiders from module 'scrapscrapy.spiders'. Check SPIDER_MODULES setting
warnings.warn(msg, RuntimeWarning)
2017-02-19 15:13:36 [scrapy.utils.log] INFO: Scrapy 1.3.2 started (bot: scrapscrapy)
2017-02-19 15:13:36 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'scrapscrapy.spiders', 'SPIDER_MODULES': ['scrapscrapy.spiders'], 'ROBOTSTXT_OBEY': True, 'BOT_NAME': 'scrapscrapy'}
Traceback (most recent call last):
File "c:\python27\Scripts\scrapy-script.py", line 11, in <module>
load_entry_point('scrapy==1.3.2', 'console_scripts', 'scrapy')()
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\cmdline.py", line 142, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\cmdline.py", line 88, in _run_print_help
func(*a, **kw)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\cmdline.py", line 149, in _run_command
cmd.run(args, opts)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\commands\crawl.py", line 57, in run
self.crawler_process.crawl(spname, **opts.spargs)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\crawler.py", line 162, in crawl
crawler = self.create_crawler(crawler_or_spidercls)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\crawler.py", line 190, in create_crawler
return self._create_crawler(crawler_or_spidercls)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\crawler.py", line 194, in _create_crawler
spidercls = self.spider_loader.load(spidercls)
File "c:\python27\lib\site-packages\scrapy-1.3.2-py2.7.egg\scrapy\spiderloader.py", line 51, in load
raise KeyError("Spider not found: {}".format(spider_name))
KeyError: 'Spider not found: ss'

最佳答案

您的 spiders 文件夹中的 __init__.py 文件似乎发生了一些问题。

尝试自己添加(留空):

───spiders
__init__.py
SSSpider.py
SSSpider.pyc

关于找不到 Python scrapy spider KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327220/

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