scrapy重新爬取数据 又报错,心累
C:\Users\Administrator\PycharmProjects\pythonProject\scrapy框架\qiubaiPro>scrapy crawl qiubai
开始爬虫。。。。。。
结束爬虫!
2020-11-02 11:24:18 [scrapy.core.engine] ERROR: Scraper close failure
Traceback (most recent call last):
File "d:\program files (x86)\python38\lib\site-packages\scrapy\crawler.py", line 89, in crawl
yield self.engine.open_spider(self.spider, start_requests)
ValueError: port should be of type int
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\program files (x86)\python38\lib\site-packages\twisted\internet\defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:\Users\Administrator\PycharmProjects\pythonProject\scrapy框架\qiubaiPro\qiubaiPro\pipelines.py", line 45, in close_spider
self.cursor.close()
AttributeError: 'NoneType' object has no attribute 'close'
2020-11-02 11:24:18 [scrapy.utils.signal] ERROR: Error caught on signal handler: <bound method CoreStats.spider_closed of <scrapy.extensions.corestats.CoreStats obje
ct at 0x00000000049E66A0>>
Traceback (most recent call last):
File "d:\program files (x86)\python38\lib\site-packages\scrapy\crawler.py", line 89, in crawl
yield self.engine.open_spider(self.spider, start_requests)
ValueError: port should be of type int
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\program files (x86)\python38\lib\site-packages\scrapy\utils\defer.py", line 164, in maybeDeferred_coro
result = f(*args, **kw)
File "d:\program files (x86)\python38\lib\site-packages\pydispatch\robustapply.py", line 55, in robustApply
return receiver(*arguments, **named)
File "d:\program files (x86)\python38\lib\site-packages\scrapy\extensions\corestats.py", line 31, in spider_closed
elapsed_time = finish_time - self.start_time
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'
Unhandled error in Deferred:
2020-11-02 11:24:18 [twisted] CRITICAL: Unhandled error in Deferred:
Traceback (most recent call last):
File "d:\program files (x86)\python38\lib\site-packages\scrapy\crawler.py", line 192, in crawl
return self._crawl(crawler, *args, **kwargs)
File "d:\program files (x86)\python38\lib\site-packages\scrapy\crawler.py", line 196, in _crawl
d = crawler.crawl(*args, **kwargs)
File "d:\program files (x86)\python38\lib\site-packages\twisted\internet\defer.py", line 1613, in unwindGenerator
return _cancellableInlineCallbacks(gen)
File "d:\program files (x86)\python38\lib\site-packages\twisted\internet\defer.py", line 1529, in _cancellableInlineCallbacks
_inlineCallbacks(None, g, status)
--- <exception caught here> ---
File "d:\program files (x86)\python38\lib\site-packages\twisted\internet\defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "d:\program files (x86)\python38\lib\site-packages\scrapy\crawler.py", line 89, in crawl
yield self.engine.open_spider(self.spider, start_requests)
builtins.ValueError: port should be of type int
2020-11-02 11:24:18 [twisted] CRITICAL:
Traceback (most recent call last):
File "d:\program files (x86)\python38\lib\site-packages\twisted\internet\defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "d:\program files (x86)\python38\lib\site-packages\scrapy\crawler.py", line 89, in crawl
yield self.engine.open_spider(self.spider, start_requests)
ValueError: port should be of type int
C:\Users\Administrator\PycharmProjects\pythonProject\scrapy框架\qiubaiPro>
1.什么是scrapy?
功能:高性能的持久化存储,异步的数据下载,高性能的数据解析,分布式
2.scrapy框架的基本使用:
环境安装:
mac或者 linux 系统安装 pip install scrapy
windows系统安装:
pip install wheel
下载 twisted : http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
安装 twisted : pip install Twisted-17.1.0-cp36-win_amd64.whl
pip install pywin32
pip install scrapy
测试;在终端里录入scrapy指令,没有报错即表示安装成功!
创建一个工程:
scrapy startproject firstBlood1
简直没办法,我实在处理不了这个报错,只有重新再学一遍
创建一个工程:
scrapy startproject firstBlood1
在spiders子目录中创建一个爬虫文件
scrapy genspider spiderName www.xxx.com
输入上面的命令之前,我们需要先进入到项目目录下: cd firstBlood1
执行工程:
scrapy crawl first
C:\Users\Administrator\PycharmProjects\pythonProject\scrapy框架\firstBlood1>scrapy crawl first
2020-11-02 12:50:45 [scrapy.utils.log] INFO: Scrapy 2.3.0 started (bot: firstBlood1)
2020-11-02 12:50:45 [scrapy.utils.log] INFO: Versions: lxml 4.5.2.0, libxml2 2.9.5, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 20.3.0, Python 3.8.3 (tags/v
3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)], pyOpenSSL 19.1.0 (OpenSSL 1.1.1h 22 Sep 2020), cryptography 3.1.1, Platform Windows-7-6.1.7601-SP
1
2020-11-02 12:50:45 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2020-11-02 12:50:45 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'firstBlood1',
'NEWSPIDER_MODULE': 'firstBlood1.spiders',
'SPIDER_MODULES': ['firstBlood1.spiders']}
2020-11-02 12:50:45 [scrapy.extensions.telnet] INFO: Telnet Password: bccd40c8412f9b20
2020-11-02 12:50:45 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2020-11-02 12:50:45 [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']
2020-11-02 12:50:45 [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']
2020-11-02 12:50:45 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2020-11-02 12:50:45 [scrapy.core.engine] INFO: Spider opened
2020-11-02 12:50:46 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-11-02 12:50:46 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-11-02 12:50:46 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://www.baidu.com/> (referer: None)
<200 http://www.baidu.com/>
2020-11-02 12:50:46 [scrapy.core.engine] INFO: Closing spider (finished)
2020-11-02 12:50:46 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 213,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 1476,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'elapsed_time_seconds': 0.265015,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2020, 11, 2, 4, 50, 46, 266980),
'log_count/DEBUG': 1,
'log_count/INFO': 10,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2020, 11, 2, 4, 50, 46, 1965)}
2020-11-02 12:50:46 [scrapy.core.engine] INFO: Spider closed (finished)
如果不想要日志信息:
scrapy crawl first --nolog
这个不显示,错误信息
所以我们得按照上面的修改一下配置文件
#显示指定类型的日志信息
LOG_LEVEL =‘ERROR’
这里的配置文件修改一下
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'