如何将Py3k HTTPResponse转储到json.load中?

时间:2012-10-02 03:03:15

标签: python json python-3.x urllib2

我认为json.load()应该能够像http.client.HTTPResponse一样读取对象,但它似乎正在绊倒它的read()是一个类似字节的对象。 (我使用的是Python 3.3。)令我惊讶的是,我发现没有资源可以直接解决这个问题,尽管我认为这是一个主要的用例。

import urllib.request, json

# Y!F url
yf = 'http://d.yimg.com/autoc.finance.yahoo.com/autoc'

# Mock lookup
data = urllib.parse.urlencode({'query': 'Ford', 'callback': 'YAHOO.Finance.SymbolSuggest.ssCallback'})
data = data.encode('utf-8')
request = urllib.request.Request(yf)
request.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14')
request.add_header('Content-type','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;charset=utf-8')
request.add_header('Accept','text/plain')

mock = urllib.request.urlopen(request, data)
json.load(mock)

这会导致错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 264, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 309, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: can't use a string pattern on a bytes-like object

1 个答案:

答案 0 :(得分:0)

这已在前一个帖子中解决:Python 3, let json object accept bytes or let urlopen output strings (HT:Matthew Trevor)

另外,雅虎!这里返回的不仅仅是JSON对象,还有YAHOO.Finance.SymbolSuggest.ssCallback()包装器。剥离修复事情。 (虽然仍然很难过,但仍然需要。)

这有效:

import urllib.request, json, re

# Y!F url
yf = 'http://d.yimg.com/autoc.finance.yahoo.com/autoc'

# Mock lookup
data = urllib.parse.urlencode({'query': 'Ford', 'callback': 'YAHOO.Finance.SymbolSuggest.ssCallback'})
data = data.encode('utf-8')
request = urllib.request.Request(yf)
response = urllib.request.urlopen(request, data)
j = json.loads(re.search(r'{.*}',response.readall().decode('utf-8')).group())