scrapy hal + json不支持的响应类型

时间:2015-03-14 17:31:29

标签: python json scrapy response hal

我正在尝试根据Firefox和Safari抓取一个HAL + Json的链接,它正在返回一个Scrapy无法识别的响应对象。

链接为https://catalogue.presto.com.au/ - 这在Chrome浏览器中显示JSON时可以正常显示,但如果我尝试使用Firefox或Safari,则会下载该文件。我打算在打开链接时怀疑Scrapy下载文件,所以它不会抓取它。

有没有人遇到类似的东西或有解决方案?

通过Shell访问

当我尝试使用终端“scrapy shell https://catalogue.presto.com.au

访问网站时
"2015-03-15 00:15:08+0700 [default] DEBUG: Crawled (200) <GET https://catalogue.presto.com.au>"

然后我尝试查看(响应)并得到此错误:

>>> view(response)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/scrapy/utils/response.py", line 86, in open_in_browser
    response.__class__.__name__)
TypeError: Unsupported response type: Response

运行Scrapy对象

def parse(self, response):
    print response.__class__
    open_in_browser(response)


2015-03-15 00:23:05+0700 [prestotv2] DEBUG: Crawled (200) <GET 

class 'scrapy.http.response.Response' (referer: None) #this line is from "print response.__class__

2015-03-15 00:23:05+0700 [prestotv2] ERROR: Spider error processing <GET https://catalogue.presto.com.au/>
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 1201, in mainLoop
        self.runUntilCurrent()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 824, in runUntilCurrent
        call.func(*call.args, **call.kw)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/defer.py", line 382, in callback
        self._startRunCallbacks(result)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/defer.py", line 490, in _startRunCallbacks
        self._runCallbacks()
    --- <exception caught here> ---
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/defer.py", line 577, in _runCallbacks
        current.result = callback(current.result, *args, **kw)
      File "/Users/nathansu/Documents/Development/Whutstream/scraping/Presto/presto/spiders/TvSpider.py", line 38, in parse
        open_in_browser(response)
      File "/Library/Python/2.7/site-packages/scrapy/utils/response.py", line 86, in open_in_browser
        response.__class__.__name__)
    exceptions.TypeError: Unsupported response type: Response

1 个答案:

答案 0 :(得分:0)

这是因为回复Content-Type等于application/hal+json。如果要解析它,请通过json.loads()加载(或使用列出的here库之一):

$ scrapy shell https://catalogue.presto.com.au/
In [1]: response.headers
Out[1]: 
{'Age': '0',
 'Cache-Control': 'max-age=300, public, s-maxage=300',
 'Content-Type': 'application/hal+json',  # HERE
 'Date': 'Sat, 14 Mar 2015 17:42:45 GMT',
 'Etag': '"834550fbc4b5fc5a188bd801c45876b7613b998b"',
 'Expires': 'Sat, 14 Mar 2015 17:47:45 GMT',
 'Last-Modified': 'Sat, 14 Mar 2015 17:42:45 GMT',
 'Server': 'Apache/2.2.3 (Red Hat)',
 'Vary': 'Accept,Accept-Encoding',
 'Via': '1.1 varnish',
 'X-Powered-By': 'PHP/5.4.15',
 'X-Varnish': '905097089'}
In [2]: import json

In [3]: json.loads(response.body)
Out[3]: 
{u'_links': {u'curies': [{u'href': u'/rels/{rel}',
    u'name': u'ooyala',
    u'templated': True}],
...
{window?}&size={size?}&discovery_profile_id={discovery_profile_id?}&exclude_videos={exclude_videos?}&offer_type={offer_type}',
   u'templated': True,
   u'title': u'Trending series'},
  u'self': {u'href': u'/'}},
 u'version': u'1.6.0.1'}