我想编写一个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
命令获取内容,但它没有用。
我该怎么办?
答案 0 :(得分:5)
Sanchit,
我建议您查看requests库以检索网址。此外,正如已经说过的(我没有使用此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文件,其中包含你发布的网站使用的所有数据。
答案 1 :(得分: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请求以提供回调包装。
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'))
答案 2 :(得分:1)
在@TimberlakeCoding&amp; amp;帮助下解决了这个问题。 @MartijnPieters。这是 -
$ wget -q -O - https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://facebook.com | grep "\"pass\": true"
如果退出状态代码为0,则表示网站不支持移动设备。
希望它有所帮助! 感谢
答案 3 :(得分: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()
获取
答案 4 :(得分:0)
任何人都像我一样来到此页面,寻找答案,API不再是“ beta”。这是一个示例:
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"
}
}
]
}