2

我认为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
4

1 回答 1

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())
于 2012-10-02T20:02:31.483 回答