AttributeError: 'module' object has no attribute 'Spider' 解决了

参照上上篇安装scrapy的文章,把scarpy的依赖项以及scrapy安装完成之后,用scrapy startproject  创建了一个爬虫项目,然后写了一个最简单的爬虫模块

保存在项目的spider目录下,代码也很简单,如下

#coding:utf-8

import scrapy


class CnblogsSpider(scrapy.Spider):
    name = "cnblogs"
    allowd_domains = ["cnblogs.com"]
    start_urls = ["http://www.cnblogs.com/qiyeboy/default.html?page=1"]
    def parse(self,response):
        pass


然后执行scrapy crawl cnblogs ,总是报一大堆错误,错误信息如下

D:\cnblogs\cnblogsSpider>scrapy crawl cnblogs
2017-08-02 19:16:34+0800 [scrapy] INFO: Scrapy 0.14.4 started (bot: cnblogsSpider)
2017-08-02 19:16:35+0800 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
Traceback (most recent call last):
File "C:\Python27\Scripts\scrapy", line 4, in
__import__('pkg_resources').run_script('Scrapy==0.14.4', 'scrapy')
File "C:\Python27\lib\site-packages\pkg_resources\__init__.py", line 743, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "C:\Python27\lib\site-packages\pkg_resources\__init__.py", line 1498, in run_script
exec(code, namespace, namespace)
File "c:\python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\EGG-INFO\scripts\scrapy", line 4, in
execute()
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\cmdline.py", line 132, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\cmdline.py", line 97, in _run_print_help
func(*a, **kw)
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\cmdline.py", line 139, in _run_command
cmd.run(args, opts)
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\commands\crawl.py", line 43, in run
spider = self.crawler.spiders.create(spname, **opts.spargs)
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\command.py", line 34, in crawler
self._crawler.configure()
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\crawler.py", line 36, in configure
self.spiders = spman_cls.from_crawler(self)
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\spidermanager.py", line 37, in from_crawler
return cls.from_settings(crawler.settings)
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\spidermanager.py", line 33, in from_settings
return cls(settings.getlist('SPIDER_MODULES'))
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\spidermanager.py", line 23, in __init__
for module in walk_modules(name):
File "C:\Python27\lib\site-packages\scrapy-0.14.4-py2.7.egg\scrapy\utils\misc.py", line 65, in walk_modules
submod = __import__(fullpath, {}, {}, [''])
File "D:\cnblogs\cnblogsSpider\cnblogsSpider\spiders\cnblogs_spider.py", line 3, in
class CnblogsSpider(scrapy.Spider):
AttributeError: 'module' object has no attribute 'Spider'



研究半天不知道为何原因,网上查找,相关的报错信息也很少,后来注意到最后一句提示:

AttributeError: 'module' object has no attribute 'Spider'----------------感觉还是scrapy的安装有问题,因为在pcCharm的IDE环境中,虽然写了import scrapy这句


但在 class CnblogsSpider(scrapy.Spider)中,spider总是高亮提示,鼠标放上去显示 cannot find refrence in '__init__.py'  ,综合以上,我判断还是scrapy的安装有问题,因为之前安装scrapy,是从别人的百度网盘下载的scrapy安装包来安装的,这次重新搜索scrapy 安装,找到了他的官网https://scrapy.org/download/,截止2017.8.3,最新版本是1.4,我之前安装的版本是0.14,看来应该是版本太低的原因,于是重新下载scrapy的最新版本1.4,重新安装,再次执行

scrapy crawl cnblogs 不再出现上述报错信息,运行完之后的提示如下:


D:\cnblogs\cnblogsSpider>scrapy crawl cnblogs
2017-08-03 06:56:51 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: cnblogsSpider)
2017-08-03 06:56:51 [scrapy.utils.log] INFO: Versions: lxml 2.3.0.0, libxml2 2.7.7, cssselect 1.0.1, parsel 1.2.0, w3lib 1.17.0, Twisted 13.1.0, Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)], pyOpenSSL 0.13.1 (OpenSSL 1.0.1e 11 Feb 2013), Platform Windows-10-10.0.14393}
2017-08-03 06:56:51 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'cnblogsSpider.spiders', 'SPIDER_MODULES': ['cnblogsSpider.spiders'], 'BOT_NAME': 'cnblogsSpider'}
2017-08-03 06:56:52 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-08-03 06:56:52 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-08-03 06:56:52 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-08-03 06:56:52 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-08-03 06:56:52 [scrapy.core.engine] INFO: Spider opened
2017-08-03 06:56:52 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-08-03 06:56:52 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-08-03 06:56:53 [scrapy.core.engine] DEBUG: Crawled (200) (referer: None)
2017-08-03 06:56:53 [scrapy.core.engine] INFO: Closing spider (finished)
2017-08-03 06:56:53 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 240,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 5168,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 8, 2, 22, 56, 53, 118000),
 'log_count/DEBUG': 2,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 8, 2, 22, 56, 52, 763000)}
2017-08-03 06:56:53 [scrapy.core.engine] INFO: Spider closed (finished)


D:\cnblogs\cnblogsSpider>


真是头疼,上次也是因为一个很简单的问题,困扰了一下午

你可能感兴趣的:(AttributeError: 'module' object has no attribute 'Spider' 解决了)