gpt4 book ai didi

python-2.7 - 运行Scrapy项目时无法导入名称

转载 作者:行者123 更新时间:2023-12-02 01:45:49 26 4
gpt4 key购买 nike

我的项目名称是 NOTHS。

以下脚本是我使用的spider.pyitems.py

spider.py:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from NOTHS.items import NOTHS
class MySpider(BaseSpider):
name = "main"
allowed_domains = ["notonthehighstreet.com"]
start_urls = ["http://www.notonthehighstreet.com"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//a[@class='title']")
items = []
for titles in titles:
item = NOTHS()
item ["title"] = titles.select("a/text()").extract()
item ["link"] = titles.select("a/@href").extract()
items.append(item)
return items

items.py:

from scrapy.item import Item, Field
class CraigslistSampleItem(Item):
title = Field()
link = Field()

当我运行它时出现以下错误:

C:\Users\ACER\Documents\works\source code\NOTHS>scrapy crawl main
:0: UserWarning: You do not have a working installation of the service_identity
module: 'No module named service_identity'. Please install it from <https://pyp
i.python.org/pypi/service_identity> and make sure all of its dependencies are sa
tisfied. Without the service_identity module and a recent enough pyOpenSSL to s
upport it, Twisted can perform only rudimentary TLS client hostname verification
. Many valid certificate/hostname mappings may be rejected.
Traceback (most recent call last):
File "C:\Python27\Scripts\scrapy-script.py", line 9, in <module>
load_entry_point('scrapy==0.24.4', 'console_scripts', 'scrapy')()
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\cmdline.py"
, line 143, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\cmdline.py"
, line 89, in _run_print_help
func(*a, **kw)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\cmdline.py"
, line 150, in _run_command
cmd.run(args, opts)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\commands\cr
awl.py", line 57, in run
crawler = self.crawler_process.create_crawler()
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\crawler.py"
, line 87, in create_crawler
self.crawlers[name] = Crawler(self.settings)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\crawler.py"
, line 25, in __init__
self.spiders = spman_cls.from_crawler(self)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\spidermanag
er.py", line 35, in from_crawler
sm = cls.from_settings(crawler.settings)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\spidermanag
er.py", line 31, in from_settings
return cls(settings.getlist('SPIDER_MODULES'))
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\spidermanag
er.py", line 22, in __init__
for module in walk_modules(name):
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\utils\misc.
py", line 68, in walk_modules
submod = import_module(fullpath)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\ACER\Documents\works\source code\NOTHS\NOTHS\spiders\main.py",
line 3, in <module>
from NOTHS.items import NOTHS
ImportError: cannot import name NOTHS

我做错了什么?

最佳答案

在文件spider.py中,更改

from NOTHS.items import NOTHS

from NOTHS.items import CraigslistSampleItem

关于python-2.7 - 运行Scrapy项目时无法导入名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25745174/

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