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:
- the url corrected (as mentioned felix kling here). have remove
callback
parameter request sending. - also, if check type of response fetching earlier you'll notice wasn't string.
<type 'instance'>
. , sincejson.loads()
acceptsstring
parameter variable would've got error. therefore, usea.read()
fetch response data instring
.
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
Post a Comment