标签:refresh 博文 inter mozilla article timeout help use 问题
一、问题描述
有时候用scrapy shell来调试很方便,但是有些网站有防爬虫机制,所以使用scrapy shell会返回403,比如下面
C:\Users\fendo>scrapy shell https://book.douban.com/subject/26805083/
2017-04-17 15:18:53 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: scrapybot)
2017-04-17 15:18:53 [scrapy.utils.log] INFO: Overridden settings: {‘DUPEFILTER_CLASS‘: ‘scrapy.dupefilters.BaseDupeFilter‘, ‘LOGSTATS_INTERVAL‘: 0}
2017-04-17 15:18:53 [scrapy.middleware] INFO: Enabled extensions:
[‘scrapy.extensions.corestats.CoreStats‘,
‘scrapy.extensions.telnet.TelnetConsole‘]
2017-04-17 15:18:54 [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.stats.DownloaderStats‘]
2017-04-17 15:18:54 [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-04-17 15:18:54 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-04-17 15:18:54 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2017-04-17 15:18:54 [scrapy.core.engine] INFO: Spider opened
2017-04-17 15:18:54 [scrapy.core.engine] DEBUG: Crawled (403) <GET https://book.douban.com/subject/26805083/> (referer: None)
2017-04-17 15:18:54 [traitlets] DEBUG: Using default logger
2017-04-17 15:18:54 [traitlets] DEBUG: Using default logger
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x000001E696FBAD68>
[s] item {}
[s] request <GET https://book.douban.com/subject/26805083/>
[s] response <403 https://book.douban.com/subject/26805083/>
[s] settings <scrapy.settings.Settings object at 0x000001E6993C7B70>
[s] spider <DefaultSpider ‘default‘ at 0x1e69964d1d0>
[s] Useful shortcuts:
[s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s] fetch(req) Fetch a scrapy.Request and update local objects
[s] shelp() Shell help (print this help)
[s] view(response) View response in a browser
In [1]:
直接返回403!!
二、解决方法
有两种解决方法:
(1):第一种方法是在命令上加上-s USER_AGENT=‘Mozilla/5.0‘
C:\Users\fendo>scrapy shell -s USER_AGENT=‘Mozilla/5.0‘ https://book.douban.com/subject/26805083/
2017-04-17 15:21:37 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: scrapybot)
2017-04-17 15:21:37 [scrapy.utils.log] INFO: Overridden settings: {‘DUPEFILTER_CLASS‘: ‘scrapy.dupefilters.BaseDupeFilter‘, ‘LOGSTATS_INTERVAL‘: 0, ‘USER_AGENT‘: "‘Mozilla/5.0‘"}
2017-04-17 15:21:37 [scrapy.middleware] INFO: Enabled extensions:
[‘scrapy.extensions.corestats.CoreStats‘,
‘scrapy.extensions.telnet.TelnetConsole‘]
2017-04-17 15:21:37 [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.stats.DownloaderStats‘]
2017-04-17 15:21:37 [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-04-17 15:21:37 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-04-17 15:21:37 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2017-04-17 15:21:37 [scrapy.core.engine] INFO: Spider opened
2017-04-17 15:21:38 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book.douban.com/subject/26805083/> (referer: None)
2017-04-17 15:21:38 [traitlets] DEBUG: Using default logger
2017-04-17 15:21:38 [traitlets] DEBUG: Using default logger
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x000001D2DC68AD68>
[s] item {}
[s] request <GET https://book.douban.com/subject/26805083/>
[s] response <200 https://book.douban.com/subject/26805083/>
[s] settings <scrapy.settings.Settings object at 0x000001D2DEAB6B38>
[s] spider <DefaultSpider ‘default‘ at 0x1d2ded3d208>
[s] Useful shortcuts:
[s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s] fetch(req) Fetch a scrapy.Request and update local objects
[s] shelp() Shell help (print this help)
[s] view(response) View response in a browser
In [1]:
第一种方法最简单但是每次操作都要加上去很繁琐,第二种方法比较好。
(2):第二种方法是修改scrapy的user-agent默认值
找到python的:安装目录下的default_settings.py文件,比如我的F:\Software\Python36\Lib\site-packages\scrapy\settings\default_settings.py
把
USER_AGENT = ‘Scrapy/%s (+http://scrapy.org)‘ % import_module(‘scrapy‘).__version__
改为
USER_AGENT = ‘Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0‘
使用shell再次,发现已经可以正常访问html不会在出现403错误了。
C:\Users\fendo>scrapy shell "https://book.douban.com/subject/26805083/"
2017-04-17 15:34:13 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: scrapybot)
2017-04-17 15:34:13 [scrapy.utils.log] INFO: Overridden settings: {‘DUPEFILTER_CLASS‘: ‘scrapy.dupefilters.BaseDupeFilter‘, ‘LOGSTATS_INTERVAL‘: 0}
2017-04-17 15:34:14 [scrapy.middleware] INFO: Enabled extensions:
[‘scrapy.extensions.corestats.CoreStats‘,
‘scrapy.extensions.telnet.TelnetConsole‘]
2017-04-17 15:34:14 [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.stats.DownloaderStats‘]
2017-04-17 15:34:14 [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-04-17 15:34:14 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-04-17 15:34:14 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2017-04-17 15:34:14 [scrapy.core.engine] INFO: Spider opened
2017-04-17 15:34:15 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book.douban.com/subject/26805083/> (referer: None)
2017-04-17 15:34:15 [traitlets] DEBUG: Using default logger
2017-04-17 15:34:15 [traitlets] DEBUG: Using default logger
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x000001476886AD68>
[s] item {}
[s] request <GET https://book.douban.com/subject/26805083/>
[s] response <200 https://book.douban.com/subject/26805083/>
[s] settings <scrapy.settings.Settings object at 0x000001476AC97B70>
[s] spider <DefaultSpider ‘default‘ at 0x1476af1d198>
[s] Useful shortcuts:
[s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s] fetch(req) Fetch a scrapy.Request and update local objects
[s] shelp() Shell help (print this help)
[s] view(response) View response in a browser
In [1]:
---------------------
作者:lfendo
来源:CSDN
原文:https://blog.csdn.net/u011781521/article/details/70211474
版权声明:本文为博主原创文章,转载请附上博文链接!
标签:refresh 博文 inter mozilla article timeout help use 问题
原文地址:https://www.cnblogs.com/jcjc/p/10443434.html