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 .
score:2
Two corrections need to be made to your code:
- The url was corrected (as mentioned by Felix Kling here). You have to remove the
callback
parameter from the GET request you were sending. - 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 sincejson.loads()
accepts astring
as a parameter variable you would've got another error. Therefore, usea.read()
to fetch the response data instring
.
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
Similar question
- Read JSON response in Python
- how to get the response json data from network call in XHR using Python selenium web driver chorme
- how to read json string from firefox response using selenium webdriver
- Scraping - catch json response with python
- Python selenium webdriver read email address from response of java script
score: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
Reference: stackoverflow.com
POPULA POST
- Capture AJAX response with selenium and python
- selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() with urls read from text file with Selenium Python
- Python - Selenium PhantomJS - JSON Error
- BrowserMob Proxy Python - How to get response body?
- Selenium Python - Get Network response body
- How to read the text from the alert box using Python + Selenium
- how to grab from JSON in selenium python
- How to get the raw JSON response of a HTTP request from `driver.page_source` in Selenium webdriver Firefox
- Parse HTML and Read HTML Table with Selenium Python
- Read & map data from Json file in c# test
- Read a hidden value in a div using Selenium Python binding
- How to get rid of response messages initiating Google Chrome using ChromeDriver in Python Selenium
- How to identify the 32 bit data-sitekey of ReCaptcha V2 to obtain a valid response programmatically using Selenium and Python Requests?
- How can I read console log (e.g. messages like ok, connected..) of particular website by using a python coding?
- How to get data from Response with Python Selenium in DevTools
- Capture JSON response through Selenium
- How to read a file downloaded by selenium webdriver in python
- Java Selenium get JSON response body
- How to read a JSON for just one time and use it many time in the same robot file in Robot Framework
- Read dynamic webpage html in either Python or R
- Selenium web scraping in python cant read .text of elements
- Unable to read downloaded file after converting python file to .exe
- http.client.RemoteDisconnected: Remote end closed connection without response error using driver.quit() of Selenium Python
- Selenium python capture response time
- Selenium IDE doesn't open webpages that return a json response
- How can i add response time in selenium python report
- I want to get orderID from API response using selenium python
- KeyError: 0 in Python When Trying to Get JSON Data
- Unable to read text from a div tag in python
- Python Selenium, how to save http response content?
- Read from Excel list and iterate/action Python Selenium
- How to click on the Read more link from the first review within tripadvisor using Selenium and Python
- Scrape JSON response with Selenium
- is there a way to able to read text from such images using selenium python
- CSV read specific row in python
- TypeError: Object of type RelativeBy is not JSON serializable error using Selenium 4's Relative Locator with Python 3
- Python - pyinstaller : Read path file inside script to use in pyinstaller
- how to read hidden text in python by using selenium?
- Why am i unable to read an xpath with selenium in python from a PowerBI webpage
- How can i use logical OR for json array response
- Python Selenium Read Values from xPath attributes
- An error has occured in 'site url': Uncaught TypeError: Cannot read property 'getColomnSet' of undefined with Selenium and Python
- Running selenium python test from soapui to read a file
- TypeError: Object of type RelativeBy is not JSON serializable error using relative_locator through Selenium Python
- Python Selenium Chrome - Message: unknown error: cannot parse internal JSON template: Line: 1, column: 1, Unexpected token
Featured post from same tag
- close all window firefox using selenium in java?
- can cucumber take screenshot for every step
- Click event not work if page resolution is changed, Robot Framework
- Get image URL from HTML5 canvas using Selenium Python
- Selenium WebDriver to match similar xpaths
- How to interact with an element with Selenium when I don't have it's id?
- Selenium RemoteWebDriver server is slow and shows weird logs
- Chrome extension disappear every time i run my automation test script
- Unable to select an element from hidden dropdown using selenium webdriver
- Python Selenium: accessing aria-label information
- Selenium Webdriver: window determined as alert (HTTP Basic Access Authentication), how to login (Python)
- I am trying to search a keyword on youtube using Selenium in python
- Selenium+Python: for.. (loop) doesn't work as expected
- is it possible to read something.properties using javascript ? (in a .hta file) >> to read in Selenium
- When i want to run this program instead of run as application it will shown like "run as configuration". Why it will ask like this