使用json解析数据时,通常遇到这里就会出现问题'bytes' object has no attribute 'read',这是由于使用的json内置函数不同,一个是load另一个是loads。

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

通常解决方式有两种,一种是更改函数为loads,另一种是更改编码格式为utf8

第一种解决方式:

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

第二种解决方式

使用json.loads()而不是json.load()

内容参考:https://stackoverflow.com/questions/6541767/python-urllib-error-attributeerror-bytes-object-has-no-attribute-read

相关文章:

  • 2022-12-23
  • 2021-05-22
  • 2022-03-11
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2021-06-17
猜你喜欢
  • 2021-11-27
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-05-13
  • 2021-08-06
相关资源
相似解决方案