在 Python 中读取 JSON 响应 [英] Read JSON response in Python

查看:72
本文介绍了在 Python 中读取 JSON 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此 link.但它不工作!我收到以下错误:

I am trying to read json response from this link. But its not working! I get the following error:

ValueError:无法解码 JSON 对象.

ValueError: No JSON object could be decoded.

这是我试过的代码:

import urllib2, json
a = urllib2.urlopen('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false&callback=_callbacks_._DElanZU7Xh1K')
data = json.loads(a)

我进行了这些更改:

import requests, json
r=requests.get('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false')
json_data = json.loads(r.text)
print json_data['ruleGroups']['USABILITY']['score']

一个简单的问题 - 构建图像链接.

A Quick question - Construct Image link .

我能够到达这里:-

from selenium import webdriver

txt = json_data['screenshot']['data']
txt = str(txt).replace('-','/').replace('_','/')
#then in order to construct the image link i tried : -
image_link = 'data:image/jpeg;base64,'+txt
driver = webdriver.Firefox()
driver.get(image_link)

问题是我没有得到图像,len(object_original) 与 len(image_link) 相比也不同.有人可以告诉我构建的图像链接中缺少的正确元素吗?谢谢

The problem is i am not getting the image, also the len(object_original) as compared len(image_link) differs . Could anybody please advise the right elements missing in my constructed image link ?. Thank you

这是 API 链接 - https://www.google.co.uk/webmasters/tools/mobile-friendly/ 抱歉添加晚了.

Here is API link - https://www.google.co.uk/webmasters/tools/mobile-friendly/ Sorry added it late .

推荐答案

需要对您的代码进行两项更正:

Two corrections need to be made to your code:

  1. 网址已更正(如 Felix Kling 此处所述)).您必须从发送的 GET 请求中删除 callback 参数.
  2. 此外,如果您检查之前获取的响应的类型,您会注意到它不是字符串.它是 .由于 json.loads() 接受一个 string 作为参数变量,你会得到另一个错误.因此,使用a.read() 来获取string 中的响应数据.
  1. The url was corrected (as mentioned by Felix Kling here). You have to remove the callback parameter from the GET request you were sending.
  2. Also, if you check the type of the response that you were fetching earlier you'll notice that it wasn't a string. It was <type 'instance'>. And since json.loads() accepts a string as a parameter variable you would've got another error. Therefore, use a.read() to fetch the response data in string.

因此,这应该是您的代码:

Hence, this should be your code:

import urllib2, json
a = urllib2.urlopen('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=https://www.economicalinsurance.com/en/&strategy=mobile&filter_third_party_resources=false')
data = json.loads(a.read())

对您的第二个查询(关于图片)的回答是:

Answer to your second query (regarding the image) is:

from base64 import decodestring

arr = json_data['screenshot']['data']
arr = arr.replace("_", "/")
arr = arr.replace("-","+")

fh = open("imageToSave.jpeg", "wb")
fh.write(str(arr).decode('base64'))
fh.close()

这是您尝试获取的图像 - 链接

Here, is the image you were trying to fetch - Link

这篇关于在 Python 中读取 JSON 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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