Skip to content

Instantly share code, notes, and snippets.

@kemsakurai
Last active July 22, 2017 07:16
Show Gist options
  • Save kemsakurai/12cdf278c780891cf5001f7ba5cade63 to your computer and use it in GitHub Desktop.
Save kemsakurai/12cdf278c780891cf5001f7ba5cade63 to your computer and use it in GitHub Desktop.
URL Testing Tools API (Beta) を実行するpython スクリプト
# -*- coding: utf-8 -
import requests
import argparse
API_URL = "https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run"
def main():
parser = argparse.ArgumentParser(description="description goes here")
parser.add_argument(
"-key", type=str, help="help text goes here. This option is required", required=True)
parser.add_argument(
"-url", type=str, help="help text goes here. This option is required", required=True)
parser.add_argument(
"-referer", type=str, help="help text goes here. This option is optional", required=False)
command_arguments = parser.parse_args()
payload = {'url': command_arguments.url}
headers = {'Content-Type': 'application/json'}
if (command_arguments.referer):
headers.update({'referer': command_arguments.referer})
response = requests.post(
API_URL + "?key=" + command_arguments.key, params=payload, headers=headers)
print("URL=" + command_arguments.url)
print("RESULT=" + response.text)
if __name__ == '__main__':
main()
# -*- coding: utf-8 -
import requests
from bs4 import BeautifulSoup
import time
SITEMAP_URL = "your_sitemap_url"
API_URL = "https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run"
API_KEY1 = "api_key1"
API_KEY2 = "api_key2"
REFERER = "your_referer"
def main():
soup = BeautifulSoup(requests.get(SITEMAP_URL).text, "xml")
urls = []
for loc in soup.find_all('loc'):
urls.append(loc.get_text())
error_urls = execute_request(urls, 5)
error_urls = execute_request(error_urls, 5)
def execute_request(urls, seconds):
rate_limit_errors = []
count = 0
for url in urls:
time.sleep(seconds)
payload = {'url': url}
headers = {'Content-Type': 'application/json', 'referer': REFERER}
remainder = count % 2
api_key = API_KEY1 if remainder == 1 else API_KEY2
response = requests.post(
API_URL + "?key=" + api_key, params=payload, headers=headers)
json_o = response.json()
if (json_o.get("mobileFriendliness")):
if ("NOT_MOBILE_FRIENDLY" == json_o.get("mobileFriendliness")):
print("URL=" + url + "|" + "RESULT:NOT_MOBILE_FRIENDLY")
print(response.text)
else:
print("URL=" + url + "|" + "RESULT:MOBILE_FRIENDLY")
else:
print("URL=" + url + "|" + "RESULT:RATE_LIMIT_ERROR")
rate_limit_errors.append(url)
print("sleep...")
time.sleep(seconds * 4)
return rate_limit_errors
if __name__ == '__main__':
main()
@kemsakurai
Copy link
Author

URL Testing Tools API (Beta) のRate Limit が厳しく、
API_KEYを2つ使用するようにしたが、意味がなく、やはりRate Limitになる。

@kemsakurai
Copy link
Author

結局、URLを1件指定して実行するようにした

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment