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

标签 python-2.7 scrapy scrapy-spider

我的项目名称是 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/

相关文章:

Scrapy `ReactorNotRestartable` : one class to run two (or more) spiders

python - scrapy list return:如何处理/提取列表的每个元素?

python - python 中的最大迭代项和关键属性

python - ttk.Treeview - 无法更改行高

python-2.7 - 带有 LIKE 搜索的 Python Tkinter 自动完成组合框?

javascript - scrapy javascript重定向页面

python - 已编辑 : How do I create a "Nested Loop" that returns an item to the original list in Python and Scrapy

python - 检查文本中是否存在大量关键字

web-scraping - 抓取错误 : 'NotSupported: Unsupported URL scheme ' ': no handler available for that scheme'

python sterling的近似程序