I wanted to write a shell/python script which will check if a website is mobile friendly or not. Using browser this can be easily done by visiting-
https://www.google.com/webmasters/tools/mobile-friendly/?url=<website_addr>
For eg.-
https://www.google.com/webmasters/tools/mobile-friendly/?url=http://facebook.com
I tried fetching the content through curl,
wget
, lynx
commands but it did not worked.
How can I do so?
The page uses a JSONP request to an as-yet unpublished Google PageSpeed API. Google publishes PageSpeeds Insights API v2, but the page appears to be using a v3beta1 endpoint.
When you go to the
https://www.google.com/webmasters/tools/mobile-friendly/?url=http://facebook.com
page for example and look at the network tab of your browser developer tools, you'll see a request for:The
url
parameter is directly taken from theurl
parameter passed to the page, thecallback
parameter is there for the JSONP request to provide a callback wrapper.There is a chance Google will swap out the API key used there, but in the meantime you can use Python code to validate the mobile friendliness of a site with:
Sanchit,
I suggest you look at the requests library for retrieving the url. Also, as has already been said (I don't have experience with this api) you need to call 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://facebook.com' instead of the url you posted.
Here's an example:
That would give you a json file with all the data that the website you posted uses.
Solved it myself, with help of @TimberlakeCoding & @MartijnPieters. Here it is-
If the exit status code is 0, that means website is mobile friendly else not.
Hope it helps someone! Thanks
I wrote a simple python script for this similar task to send multiple network requests to google Mobile-Friendly Test api and save "pass" and some other fields to mysql db. It's very fast and efficient.
Also available at https://github.com/abm-adnan/multiple-requests