移动友好测试脚本

Script to Mobile-Friendly test

我想编写一个 shell/python 脚本来检查网站是否适合移动设备。使用浏览器可以很容易地通过访问-

https://www.google.com/webmasters/tools/mobile-friendly/?url=<website_addr>

例如-

https://www.google.com/webmasters/tools/mobile-friendly/?url=http://facebook.com

我尝试通过 curl, wgetlynx 命令获取内容,但没有成功。

我该怎么做?

桑吉特,

我建议您查看 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' 而不是您发布的 url。

这是一个例子:

import requests

r = requests.get('https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://facebook.com')

data = r.json()

这将为您提供一个 json 文件,其中包含您发布的网站使用的所有数据。

在@TimberlakeCoding 和@MartijnPieters 的帮助下,我自己解决了这个问题。在这里-

$ wget -q -O - https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://facebo‌​ok.com | grep "\"pass\": true" 

如果退出状态代码为 0,则表示网站适合移动设备,否则不适合。

希望对大家有所帮助! 谢谢

该页面对尚未发布的 Google PageSpeed API 使用 JSONP 请求。 Google 发布 PageSpeeds Insights API v2,但页面似乎使用了 v3beta1 端点。

例如,当您转到 https://www.google.com/webmasters/tools/mobile-friendly/?url=http://facebook.com 页面并查看浏览器开发人员工具的网络选项卡时,您会看到一个请求:

https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?key=AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA&screenshot=true&snapshots=true&locale=en_US&url=http%3A%2F%2Ffacebook.com%2F&strategy=mobile&filter_third_party_resources=false&callback=_callbacks_._Ce2bYp0wchLY

url参数直接取自传给页面的url参数,callback参数是为JSONP请求提供回调包装器。

Google 可能会换掉那里使用的 API 密钥,但与此同时,您可以使用 Python 代码来验证网站的移动友好性:

import requests

url_to_test = 'http://facebook.com'

params = {
    'key': 'AIzaSyDkEX-f1JNLQLC164SZaobALqFv4PHV-kA',
    'url': url_to_test,
}
api_url = 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady'
response = requests.get(api_url, params=params)
data = response.json()
passed = all(rule['pass'] for rule in data['ruleGroups'].values())

print('{} is {}'.format(url_to_test, 'mobile friendly' if passed else 'not mobile friendly'))

我为这个类似的任务编写了一个简单的 python 脚本,用于将多个网络请求发送到 google 移动设备友好测试 api 并保存 "pass" 和一些其他字段到 mysql 分贝。它非常快速和高效。

# download mysql connector for python 
# from: https://dev.mysql.com/downloads/connector/odbc/
# select your Platform from drop-down and install it

    from twisted.internet import reactor, threads
    from urlparse import urlparse
    import httplib
    import itertools
    import json
    import mysql.connector

    GOOGLE_API_KEY = 'YOUR GOOGLE API KEY HERE'

    db = mysql.connector.connect(user='root', password='root',
                                  host='127.0.0.1',
                                  database='mobiletracker', autocommit=True)


    cursor = db.cursor()

    concurrent = 10
    finished=itertools.count(1)
    reactor.suggestThreadPoolSize(concurrent)

    def getData(ourl):
        googleapiUrl = 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=' + ourl + '&key=' + GOOGLE_API_KEY
        print googleapiUrl
        url = urlparse(googleapiUrl)
        conn = httplib.HTTPSConnection(url.netloc)   
        conn.request("GET", url.path + '?' + url.query)
        res = conn.getresponse()
        return res.read()

    def processResponse(response,url):
        jsonData = json.loads(response)
        try:
          score = str(jsonData['ruleGroups']['USABILITY']['score'])
        except Exception, e:
          score = '0'
        try:
          pass_ = jsonData['ruleGroups']['USABILITY']['pass']  #Boolean
          if pass_:
            pass_ = '1'
          else:
            pass_ = '0'
        except Exception, e:
          pass_ = '0'
        try:
          cms = str(jsonData['pageStats']['cms'])
        except Exception, e:
          cms = ''

        cursor.execute("SELECT id FROM mobile WHERE url='" + url + "'")
        result = cursor.fetchone()
        try:
          id_ = str(result[0])
          query = "UPDATE mobile SET score='" + score + "', pass='" + pass_ + "', cms='" + cms + "' WHERE id = '" + id_ + "'"
          print query
          cursor.execute(query)
        except Exception, e:
          query = "INSERT INTO mobile SET url='" + url + "', score='" + score + "', pass='" + pass_ + "', cms='" + cms + "'"
          print query
          cursor.execute(query)
        processedOne()

    def processError(error,url):
        print "error", url, error
        processedOne()

    def processedOne():
        if finished.next()==added:
            reactor.stop()

    def addTask(url):
        req = threads.deferToThread(getData, url)
        req.addCallback(processResponse, url)
        req.addErrback(processError, url)   

    added=0
    for url in open('urllist.csv'):
        added+=1
        addTask(url.strip())

    try:
        reactor.run()
    except KeyboardInterrupt:
        reactor.stop()

也可在 https://github.com/abm-adnan/multiple-requests

任何像我一样来到此页面寻找答案的人,API 不再是“测试版”。这是一个例子:

curl -H 'Content-Type: application/json' --data '{url: "https://URL_OF_WEBSITE.COM/"}' 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key=YOUR_API_KEY'

然后,它将return JSON像这样:

{
  "testStatus": {
    "status": "COMPLETE"
  },
  "mobileFriendliness": "MOBILE_FRIENDLY",
  "resourceIssues": [
    {
      "blockedResource": {
        "url": "https://assist.zoho.com/login/embed-remote-support.jsp"
      }
    }
  ]
}