Read JSON response in Python -


i trying read json response link. not working! following error:

valueerror: no json object decoded.

here code i've tried:

import urllib2, json = 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 here : -

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

the problem not getting image, len(object_original) compared len(image_link) differs . please advise right elements missing in constructed image link ?. thank you

here api link - https://www.google.co.uk/webmasters/tools/mobile-friendly/ sorry added late .

two corrections need made code:

  1. the url corrected (as mentioned felix kling here). have remove callback parameter request sending.
  2. also, if check type of response fetching earlier you'll notice wasn't string. <type 'instance'>. , since json.loads() accepts string parameter variable would've got error. therefore, use a.read() fetch response data in string.

hence, should code:

import urllib2, json = 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 second query (regarding 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, image trying fetch - link


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -