

























维护个人网站有一部分工作是让搜索引擎快速收录网页内容,获得自然搜索流量服务更多用户。直接去 Google 等搜索引擎提交是一种方式,使用 API 直接提交是另外一种方式,当你一次想提交多个网址的时候,API 的方式更加方便。
Google index api 是和 google 云平台上管理的,这一步过程省略(网络上有很多相关教程),设置成功后会下载一个 json 文件存到本地,文件里面是一些密钥信息,通过这个密钥文件,才能获得授权调用 Google 相关的 api

准备环境,安装依赖包
1pip install --upgrade google-api-python-client oauth2client
文件预览
1(.venv) ➜ google-api-python-client tree
2.
3├── google_batch.py
4├── google_index.py
5├── hugo-368210-30b9660ab8b3.json
6├── readme.txt
7└── urls.txt
8
90 directories, 5 files
google_batch.py 用于批量提交
提交收录一般为URL_UPDATED,当然也可以删除,有需要可查看对应的官方文档。
程序文件内容如下:
1from oauth2client.service_account import ServiceAccountCredentials
2from googleapiclient.discovery import build
3from googleapiclient.http import BatchHttpRequest
4import httplib2
5
6
7with open("urls.txt") as f:
8 new_urls = f.readlines()
9
10
11JSON_KEY_FILE = "hugo-368210-30b9660ab8b3.json"
12SCOPES = ["https://www.googleapis.com/auth/indexing"]
13ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
14
15# Authorize credentials
16credentials = ServiceAccountCredentials.from_json_keyfile_name(
17 JSON_KEY_FILE, scopes=SCOPES
18)
19http = credentials.authorize(httplib2.Http())
20
21# Build service
22service = build("indexing", "v3", credentials=credentials)
23
24
25def insert_event(request_id, response, exception):
26 if exception is not None:
27 print(exception)
28 else:
29 print(response)
30
31
32batch = service.new_batch_http_request(callback=insert_event)
33
34# url updated
35for url in new_urls:
36 batch.add(
37 service.urlNotifications().publish(body={"url": url, "type": "URL_UPDATED"})
38 )
39
40batch.execute()
google_index.py 单个网址提交
文件里面的content部分就是你要提交的网址信息和操作类型。
程序示例:
1
2from oauth2client.service_account import ServiceAccountCredentials
3import httplib2
4
5SCOPES = ["https://www.googleapis.com/auth/indexing"]
6ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
7
8# service_account_file.json is the private key that you created for your service account.
9JSON_KEY_FILE = "hugo-368210-30b9660ab8b3.json"
10
11credentials = ServiceAccountCredentials.from_json_keyfile_name(
12 JSON_KEY_FILE, scopes=SCOPES
13)
14
15http = credentials.authorize(httplib2.Http())
16
17# Define contents here as a JSON string.
18# This example shows a simple update request.
19# Other types of requests are described in the next step.
20
21content = """{
22\"url\": \"http://mephisto.cc/zh-tw/tech/s2t/\",
23\"type\": \"URL_UPDATED\"
24}"""
25
26response, content = http.request(ENDPOINT, method="POST", body=content)
27print("response:", response)
28print("content:", content)
hugo-368210-30b9660ab8b3.json 密钥文件
这个是创建谷歌云服务帐户后下载到本地的,里面有敏感信息,value 部分已经手动删除了, 仅供参考。
示例:
1{
2 "type": "service_account",
3 "project_id": "",
4 "private_key_id": "",
5 "private_key": "",
6 "client_email": "",
7 "client_id": "",
8 "auth_uri": "",
9 "token_uri": "",
10 "auth_provider_x509_cert_url": "",
11 "client_x509_cert_url": ""
12}
urls.txt 批量网址文件
这个是我自己习惯的网址存放方式,一行一个网址,示例:
1(.venv) ➜ google-api-python-client cat urls.txt
2https://mephisto.cc//note/hero-chen/
3https://mephisto.cc/zh-tw/note/hero-chen/
上面就是一个文章的简体地址和繁体地址,单文件更新的时候不需要这个文件,也可以改成自己需要组织格式,懂 python 的自己修改下 google_batch.py文件即可。
修改urls.txt,这一步可以自己通过程序自动生成
运行程序
示例
1(.venv) ➜ google-api-python-client python google_batch.py
2{'urlNotificationMetadata': {'url': 'https://mephisto.cc/tech/google-index-api-python/\n', 'latestUpdate': {'url': 'https://mephisto.cc/tech/google-index-api-python/\n', 'type': 'URL_UPDATED', 'notifyTime': '2023-04-10T01:48:20.759700010Z'}}}
3{'urlNotificationMetadata': {'url': 'https://mephisto.cc/zh-tw/tech/google-index-api-python/\n', 'latestUpdate': {'url': 'https://mephisto.cc/zh-tw/tech/google-index-api-python/\n', 'type': 'URL_UPDATED', 'notifyTime': '2023-04-10T01:48:20.759685330Z'}}}
单文件提交就不操作了,当你提交收录请求后,Google 会逐步处理,比百度快多了。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。