Python;urllib 错误:AttributeError:'bytes' 对象没有属性 'read' [英] Python; urllib error: AttributeError: 'bytes' object has no attribute 'read'

查看:70
本文介绍了Python;urllib 错误:AttributeError:'bytes' 对象没有属性 'read'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是 Python 3,没有 urllib2.另外,我尝试使用 json.loads(),但出现此错误:

Note: This is Python 3, there is no urllib2. Also, I've tried using json.loads(), and I get this error:

TypeError: can't use a string pattern on a bytes-like object

如果我使用 json.loads() 并从响应中删除 .read(),我会收到此错误:

I get this error if I use json.loads() and remove the .read() from response:

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'])

不起作用...我不知道为什么.

Does not work... I have no idea why.

推荐答案

试试这个:

jsonResponse = json.loads(response.decode('utf-8'))

这篇关于Python;urllib 错误:AttributeError:'bytes' 对象没有属性 'read'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆