1

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

ValueError: No JSON object could be decoded.

Here is the code I've tried:

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)

I made these changes:

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 .

I able to get here : -

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)

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

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

Termininja
  • 6,620
  • 12
  • 48
  • 49
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25
  • 2
    I assume the server returns JSONP because of `callback=_callbacks_._DElanZU7Xh1K`. Remove it and it will likely return JSON. – Felix Kling Oct 22 '15 at 13:23
  • did you have a look at the type and the format of `a`? – hiro protagonist Oct 22 '15 at 13:24
  • Can you see if this works: `data = json.loads(a.read())` – ryekayo Oct 22 '15 at 13:26
  • Thanks felix-kling , removing the 'callback' parameter from url solved my problem , I will modify the requests parameter 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'] Could add your answer below, i will accept it now . – Shekhar Samanta Oct 22 '15 at 13:44

2 Answers2

2

Two corrections need to be made to your code:

  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

Community
  • 1
  • 1
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
0

Felix Kling is right about the address, but I also created a variable that holds the URL. You can try this out to and it should work:

import urllib2, json
url = "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"
response = urllib2.urlopen(url)
data = json.loads(response.read())
print data
ryekayo
  • 2,341
  • 3
  • 23
  • 51