python - 使用 Python 的 Google 索引 API
问题描述
这是我使用 python 请求谷歌索引的代码片段。
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
# service_account_file.json is the private key that you created for your service account.
JSON_KEY_FILE = "myJSON.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
# Define contents here as a JSON string
content = """
{
"url": "https://locksro.weebly.com/",
"type": "URL_UPDATED"
}"""
#send the request
response, content = http.request(ENDPOINT, method="POST", body=content)
print(response.status)
为了确保链接索引,我通过在谷歌搜索 {site:locksro.weebly.com} 进行检查,但没有结果。我无法弄清楚问题,因为 response.status 是 200
解决方案
它对我有用
import httplib2
SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
# service_account_file.json is the private key that you created for your service account.
JSON_KEY_FILE = "myJSON.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
# Define contents here as a JSON string.
# This example shows a simple update request.
# Other types of requests are described in the next step.
content = """{
"url": "https://mysmartpros.com/tutor-tuition-online-jobs/economics",
"type": "URL_UPDATED"
}"""
response, content = http.request(ENDPOINT, method="POST", body=content)
print(response)
print(content)```
{'content-type': 'application/json; charset=UTF-8', 'vary': 'Origin, X-Origin, Referer', 'date': 'Mon, 25 Jan 2021 23:48:59 GMT', 'server': 'ESF', 'cache-control': 'private', 'x-xss-protection': '0', 'x-frame-options': 'SAMEORIGIN', 'x-content-type-options': 'nosniff', 'transfer-encoding': 'chunked', 'status': '200', 'content-length': '299', '-content-encoding': 'gzip'}
b'{\n "urlNotificationMetadata": {\n "url": "https://mysmartpros.com/tutor-tuition-online-jobs/economics",\n "latestUpdate": {\n "url": "https://mysmartpros.com/tutor-tuition-online-jobs/economics",\n "type": "URL_UPDATED",\n "notifyTime": "2021-01-25T23:48:59.783031464Z"\n }\n }\n}\n'
推荐阅读
- firebase - 我怎样才能从 Flutter 中的 firestore 只获取我的消息
- node.js - 错误:Route.get() 需要一个回调函数,但得到一个 [object String]
- swift - 在按钮上单击从 UITableView 中删除元素 - 基本问题
- python - 如何更改熊猫的列类型
- javascript - 地图和地图异步之间的区别
- docker - Apache Proxy 背后的 Gitlab-runner 交互式 Web 终端
- flutter - Flutter floatingActionButton 在模态底部(无脚手架)
- c++ - 从 QMake 到 CMake。(不同的路径级别)
- javascript - 使用 jQuery 在第一次单击时展开 div 并在第二次单击时淡出 div
- c# - 当 Windows 设置为英语区域格式时,空格字符的格式会很奇怪