Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Website Mobile Friendly Tester Automation Script Python Codes For Mobile Friendly Test

Hey Guys, I am back again with another script that may pronounce useful to website owners, search engine optimization experts as well as normal people like me. Through the codes we write and discuss in this article, you will be able to check if a website is Mobile Friendly or not. Well, here I offer a bonus. Through the codes you will be able to issue a number of websites for a mobile friendly test instantly at a time. Why is it necessary? Here’s the answer. As of the latest update in google’s search algorithm, the search engine lord now considers mobile friendliness as a major ranking factor for a website.

Python script to automate mobile friendly test

Before we begin

Before we begin our coding, let me make few things clear. We will be writing 2 files although one will be a simple text file and another will be a python file. In this text file we will write the names of the domain we want to issue for a mobile friendly test, one in each line in the format domain.com i.e without www

from json import loads
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]    

with open('websitesformobilefriendlytest.txt') as f:
    for line in f:
        google_results = br.open("https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://" + str(line)).read()
        json_obj = loads(google_results)
        if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
            print "Congrats " + str(line)  + " is mobile friendly"
        else:
            print str(line) + " is not mobile friendly"

1. Line 1 and 2

These are the import statements as we will be using mechanize module to query the mobile friendly test via a browser instantiated by the module and the response is a JSON hence we import loads from json.

2. Line 3 to 5

On line 3 we use the Browser() method of mechanize to instantiate a browser. Line 4 is a statement that tells to ignore the robots.txt file. On line 5, we specify a user agent.

3. Line 7 to 14

Line 7 opens the text file where we previously stored the names of the domain. We now can reference the content of the file via variable f.

Line 8 is the start of the for loop which stores the name of the domain in the variable line on each iteration.

On line 9, we query a domain name/ website for a mobile friendly test. The specified url will return a response of the test result which we store in a variable google_results

On line 10, we read the response and load it as a json object to a variable json_obj.

Now on line 11, we have a conditional statement to check if the website passed the mobile friendly test. The test result is a boolean value which is a value for the key “pass” which is again a value for the key “USABILITY” which in turn is a value for the key “ruleGroups” in the json_obj. Below is the example of how it may look.

{“ruleGroups” : {“USABILITY” : {“pass” : Ture/False}}}

If the website passed the mobile friendly test, the value will be True else False. Based on the result, we then print whether a website is mobile compatible or not.

Mobile friendly tester which writes result to google spreadsheet

Well, here is the bonus code. Let me know if you have any questions regarding the codes in the comment section below. Also, here’s a similar program (Is it a wordpress website checker script)with explanation on the codes which can help you understand and implement these codes. Thanks for reading

from json import loads
import mechanize
import gdata.spreadsheet.service
import datetime
rowdict = {}
rowdict['date'] = str(datetime.date.today())
spread_sheet_id = '13mX6ALRRtGlfCzyDNCqY-G_AqYV4TpE7rq1ZNNOcD_Q'
worksheet_id = 'od6'
client = gdata.spreadsheet.service.SpreadsheetsService()
client.debug = True
client.email = '[email protected]'
client.password = 'password'
client.source = 'mobilefriendlytest'
client.ProgrammaticLogin()

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]

with open('websitesformobilefriendlytest.txt') as f:
for line in f:
    google_results = br.open("https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://" + str(line)).read()
    json_obj = loads(google_results)
    rowdict['website'] = str(line)
    if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
        #print "Congrats " + str(line) + " is mobile friendly"
        rowdict['ismobilefriendly'] = "yes"
    else:
        #print str(line) + " is not mobile friendly"
        rowdict['ismobilefriendly'] = "no"
    client.InsertRow(rowdict,spread_sheet_id, worksheet_id)


This post first appeared on The Tara Nights - Coders Code, Programers AutomateThe Tara Nights, please read the originial post: here

Share the post

Website Mobile Friendly Tester Automation Script Python Codes For Mobile Friendly Test

×

Subscribe to The Tara Nights - Coders Code, Programers Automatethe Tara Nights

Get updates delivered right to your inbox!

Thank you for your subscription

×