我目前正在使用索引API v3。你知道吗
在循环中使用此API时,出现以下错误:
“属性无效。”“url”不是标准的url格式
但我很确定我的网址是正确的,因为它是从谷歌搜索控制台下载的:
代码如下:
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import json
import pandas as pd
JSON_KEY_FILE = "key.json"
SCOPES = ["https://www.googleapis.com/auth/indexing"]
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
# This file contains 2 column, URL and date
csv = pd.read_csv("my_data.csv")
csv[["URL"]][0:10].apply(lambda x: indexURL(x.to_string(), http), axis=1)
def indexURL(url, http):
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
content = {}
content['url'] = url
content['type'] = "URL_UPDATED"
json_ctn = json.dumps(content)
response, content = http.request(ENDPOINT, method="POST", body=json_ctn)
result = json.loads(content.decode())
if("error" in result):
print("Error({} - {}): {}".format(result["error"]["code"], result["error"]["status"], result["error"]["message"]))
else:
print("urlNotificationMetadata.url: {}".format(result["urlNotificationMetadata"]["url"]))
print("urlNotificationMetadata.latestUpdate.url: {}".format(result["urlNotificationMetadata"]["latestUpdate"]["url"]))
print("urlNotificationMetadata.latestUpdate.type: {}".format(result["urlNotificationMetadata"]["latestUpdate"]["type"]))
print("urlNotificationMetadata.latestUpdate.notifyTime: {}".format(result["urlNotificationMetadata"]["latestUpdate"]["notifyTime"]))
以下是URL示例列表:
有人能告诉我我的代码有什么问题吗?你知道吗
非常感谢你的帮助。你知道吗
似乎即使我对每一行应用
.strip()
,在每个URL的末尾仍然有一个\n
。你知道吗因此,我没有将一行一行地放到lambda,而是将整个系列放到lambda,并使用for循环来处理它。你知道吗
整个工作示例如下:
Google Indexing API v3 Working Example with Python 3
相关问题 更多 >
编程相关推荐