使用谷歌索引API索引URL

2024-10-01 00:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用这个guide创建一个Python程序来索引我网页中的URL。 我已经测试了指南中提供的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 = "service_account_file.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://www.generawork.tk/planillas-de-luz-ecuador-2020/",
  \"type\": \"URL_UPDATED\"
}"""

response, content = http.request(ENDPOINT, method="POST", body=content)

输出如下:

b'{\n  "urlNotificationMetadata": {\n    "url": "https://www.generawork.tk/planillas-de-luz-ecuador-2020/",\n    "latestUpdate": {\n      "url": "https://www.generawork.tk/planillas-de-luz-ecuador-2020/",\n      "type": "URL_UPDATED",\n      "notifyTime": "2020-09-03T19:49:05.614934282Z"\n    }\n  }\n}\n'

我一直在等待URL被索引,但什么都没有发生,这让我觉得使用这个API,URL只能被更新或删除

是否可以首次使用此API为URL编制索引? 然后怎么做


Tags: httpsjsonurlwwwservicedeaccountcontent