【发布时间】:2011-09-26 08:57:00
【问题描述】:
注意:这是 Python 3,没有 urllib2。另外,我试过使用 json.loads(),我得到这个错误:
TypeError: can't use a string pattern on a bytes-like object
如果我使用 json.loads() 并从响应中删除 .read(),我会收到此错误:
TypeError: expected string or buffer
>
import urllib.request
import json
response = urllib.request.urlopen('http://www.reddit.com/r/all/top/.json').read()
jsonResponse = json.load(response)
for child in jsonResponse['data']['children']:
print (child['data']['title'])
不起作用...我不知道为什么。
【问题讨论】:
-
以什么方式不起作用?试试
urllib2.urlopen -
使用
http.client:docs.python.org/3/library/http.client.html#examples,我省了很多麻烦
标签: python python-3.x urllib