3

我想编写一个 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, wget,lynx命令获取内容,但没有成功。

我该怎么做?

4

5 回答 5

5

桑吉特,

我建议您查看用于检索 url 的请求库。另外,正如已经说过的(我没有使用此 api 的经验),您需要调用“ https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://facebook.com ”您发布的网址。

这是一个例子:

import requests

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

data = r.json()

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

于 2015-02-19T21:19:11.973 回答
2

该页面对尚未发布的 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请求提供回调包装器。

谷歌有可能会换掉那里使用的 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'))
于 2015-02-20T08:16:25.267 回答
1

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

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

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

希望它可以帮助某人!谢谢

于 2015-02-19T23:43:01.577 回答
0

我为这个类似的任务编写了一个简单的 python 脚本,将多个网络请求发送到 google Mobile-Friendly Test api,并将“pass”和其他一些字段保存到 mysql db。它非常快速和高效。

# 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获得

于 2015-05-05T05:49:28.853 回答
0

任何人像我一样来到这个页面,寻找答案,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'

然后,它将像这样返回 JSON:

{
  "testStatus": {
    "status": "COMPLETE"
  },
  "mobileFriendliness": "MOBILE_FRIENDLY",
  "resourceIssues": [
    {
      "blockedResource": {
        "url": "https://assist.zoho.com/login/embed-remote-support.jsp"
      }
    }
  ]
}
于 2020-12-04T10:03:29.350 回答