当前位置: 动力学知识库 > 问答 > 编程问答 >

Does Google offer API access to the mobile friendly test?

问题描述:

Is there an API that allows access to Google's Mobile Friendly Test which can be seen at https://www.google.com/webmasters/tools/mobile-friendly/?

网友答案:

If you can't find one by googling, it probably doesn't exist.

A hacky solution would be to create a process with PhantomJS that inputs the url, submits it, and dirty-checks the dom for results.

PhantomJS is a headless WebKit scriptable with a JavaScript API.

However, if you abuse this, there is a chance that google will blacklist your ip address. Light use should be fine. Also be aware that google can change their dom structure or class names at any time, so don't be surprised if your tool suddenly breaks.

Here is some rough, untested code...

var url = 'https://www.google.com/webmasters/tools/mobile-friendly/';
page.open(url, function (status) {

  // set the url
  document.querySelector('input.jfk-textinput').value = "http://thesite.com";
  document.querySelector('form').submit();

  // check for results once in a while
  setInterval(function(){
    var results = getResults(); // TODO create getResults
    if(results){
      //TODO save the results
      phantom.exit();
    }
  }, 1000);
});
网友答案:

There is an option in pagespeed api

https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url={url}&key={api key}

key can be obtained form google cloud platform.

网友答案:

Acquire a PageSpeed Insights API KEY in https://console.developers.google.com/apis/api/pagespeedonline-json.googleapis.com/overview?project=citric-program-395&hl=pt-br&duration=P30D and create a credentials, follow the google's instructions.

In C# (6.0) and .NET 4.5.2, I did some like this: (add in your project a reference for Newtonsoft.Json.)

String yourURL = "https://www.google.com.br";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://www.googleapis.com");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync($"/pagespeedonline/v3beta1/mobileReady?url={yourURL }&key=AIzaSyArsacdp79HPFfRZRvXaiLEjCD1LtDm3ww").Result;
string json = response.Content.ReadAsStringAsync().Result;
JObject obj = JObject.Parse(json);
bool isMobileFriendly = obj.Value<JObject>("ruleGroups").Value<JObject>("USABILITY").Value<bool>("pass");
网友答案:

There is an API (Beta) for the Mobile Friendly-Test. (Release Date: 31.01.2017).

The API test outputs has three statuses:

  1. MOBILE_FRIENDLY_TEST_RESULT_UNSPECIFIED Internal error when running this test. Please try running the test again.
  2. MOBILE_FRIENDLY The page is mobile friendly. 3.NOT_MOBILE_FRIENDLY The page is not mobile friendly.

Here are more informations: https://developers.google.com/webmaster-tools/search-console-api/reference/rest/v1/urlTestingTools.mobileFriendlyTest/run

分享给朋友:
您可能感兴趣的文章:
随机阅读: