python 基础学习中对于scrapy的使用遇到了一些问题。
首先进行的是对Amazon.cn的检索结果页进行爬取,很顺利,无碍。
下一个目标是对baidu的搜索结果进行爬取
1,反爬虫
1.1 我先对ROBOTSTXT_OBEY进行设置,结果找到了scrapy的默认参数(这里涉及多Python环境下库的调用问题,又是一个坑,另写一篇进行记录)。修改无效。
询问后才知道是对scrapy startproject编译过的项目中的setting进行修改
1.2 反爬虫第二关:网址,之前调用的
import scrapy
import sys class Jizhi(scrapy.Spider): name = 'jizhi' arg = ' '.join(sys.argv[3:]) base_urls = "https://www.baidu.com" start_urls = [
'https://www.baidu.com/s?{}'.format(arg)
] counter = 0 def parse(self, response): result_lst = response.css("h3.t a::text") for result in result_lst:
yield {
'title':result.extract()
} Jizhi.counter +=1 # next_page:css('a.n') if Jizhi.counter == 5: return next_page = response.css('a.n::attr(href)') if len(next_page) == 2:
next_page_url = next_page[1].extract()
else:
next_page_url = next_page.extract_first() yield scrapy.Request(self.base_urls + next_page_url, callback=self.parse)
使用https
结果
2018-07-26 09:37:28 [scrapy.utils.log] INFO: Scrapy 1.5.0 started (bot: jizhi)
2018-07-26 09:37:28 [scrapy.utils.log] INFO: Versions: lxml 4.2.1.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.4.0, w3lib 1.19.0, Twisted 17.5.0, Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.0.2k 26 Jan 2017), cryptography 2.2.2, Platform Windows-10-10.0.17134-SP0
2018-07-26 09:37:28 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'jizhi', 'NEWSPIDER_MODULE': 'jizhi.spiders', 'SPIDER_MODULES': ['jizhi.spiders']}
2018-07-26 09:37:29 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-07-26 09:37:29 [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']
2018-07-26 09:37:29 [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']
2018-07-26 09:37:29 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-07-26 09:37:29 [scrapy.core.engine] INFO: Spider opened
2018-07-26 09:37:29 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-07-26 09:37:29 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-07-26 09:37:29 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.baidu.com/s?wd=jizhi> (referer: None)
2018-07-26 09:37:29 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.baidu.com/s?wd=jizhi> (referer: None)
Traceback (most recent call last):
File "C:\Users\asus\Anaconda3\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback
yield next(it)
File "C:\Users\asus\Anaconda3\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 30, in process_spider_output
for x in result:
File "C:\Users\asus\Anaconda3\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 339, in <genexpr>
return (_set_referer(r) for r in result or ())
File "C:\Users\asus\Anaconda3\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr>
return (r for r in result or () if _filter(r))
File "C:\Users\asus\Anaconda3\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr>
return (r for r in result or () if _filter(r))
File "C:\Users\asus\Documents\GitHub\spider\jizhi\jizhi\spiders\jizhi.py", line 40, in parse
yield scrapy.Request(self.base_urls + next_page_url, callback=self.parse)
TypeError: must be str, not NoneType
2018-07-26 09:37:29 [scrapy.core.engine] INFO: Closing spider (finished)
2018-07-26 09:37:29 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 222,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 872,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 7, 26, 1, 37, 29, 585191),
'log_count/DEBUG': 2,
'log_count/ERROR': 1,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'spider_exceptions/TypeError': 1,
'start_time': datetime.datetime(2018, 7, 26, 1, 37, 29, 324887)}
2018-07-26 09:37:29 [scrapy.core.engine] INFO: Spider closed (finished)
报错,范围值应该是str而不是NoneType,很疑惑为啥是NoneType
这一项是str,那说明后面的参数返回值为空,没爬到数据。
ERROR: Spider error processing <GET https://www.baidu.com/s?wd=jizhi> (referer: None)
最后问来问去,是https的问题,把它换成http,OK了。