为什么我的scrapy响应体返回一个favicon.ico

时间:2017-12-04 12:49:40

标签: python scrapy

我在网上搜索了很长时间。但没用。我尝试了一些网,但有一个网不能通过,它会返回favicon.ico

例如,我尝试从scrapy shell抓取以下URL:

$ scrapy shell "https://baijia.baidu.com"
2017-12-04 20:37:51 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: tutorial)
2017-12-04 20:37:51 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'tutorial.spiders', 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter', 'SPIDER_MODULES': ['tutorial.spiders'], 'HTTPCACHE_ENABLED': True, 'BOT_NAME': 'tutorial', 'LOGSTATS_INTERVAL': 0, 'FEED_EXPORT_ENCODING': 'utf-8'}
2017-12-04 20:37:51 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2017-12-04 20:37:51 [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',
 'scrapy.downloadermiddlewares.httpcache.HttpCacheMiddleware']
2017-12-04 20:37:51 [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-12-04 20:37:51 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-12-04 20:37:51 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-12-04 20:37:51 [scrapy.core.engine] INFO: Spider opened
2017-12-04 20:37:51 [scrapy.extensions.httpcache] DEBUG: Using filesystem cache storage in /home/suocheng/My_Scrapy/tutorial/.scrapy/httpcache
2017-12-04 20:37:51 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://baijia.baidu.com> (referer: None) ['cached']
[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x2ebbc10>
[s]   item       {}
[s]   request    <GET https://baijia.baidu.com>
[s]   response   <200 https://baijia.baidu.com>
[s]   settings   <scrapy.settings.Settings object at 0x2ebbb90>
[s]   spider     <DefaultSpider 'default' at 0x33634d0>
[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
>>> response.body
'favicon.ico'
>>> 

它返回favicon.ico。 如果我像这样设置Xpath:

>>> response.xpath('/html/head/title')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/scrapy/http/response/__init__.py", line 105, in xpath
    raise NotSupported("Response content isn't text")
NotSupported: Response content isn't text
那么如何解决这个问题呢? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您的favicon.ico

不正确,此服务器似乎会发送包含字符串USER-AGENT的文件

尝试使用看似正确USER-AGENT的额外选项,然后获得HTML个文件。

scrapy shell -s USER_AGENT="Mozilla/5.0" https://baijia.baidu.com

然后response.xpath将起作用。

In [1]: response.xpath('/html/head/title/text()').extract_first()
Out[1]: '首页-百家号'

您可以使用页面httpbin.org

从浏览器中获得更好的USER-AGENT