如何修复 Google 索引 API 内循环问题?
How to fix Google Indexing API inside loop problem?
我有一个 Google 索引 API 有效的代码。代码如下:
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 = "/content/astute-tractor-329613-1baed60ec1c0.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
content = """{
"url": "https://sitename.com/index.php",
"type": "URL_UPDATED"
}"""
response, content = http.request(ENDPOINT, method="POST", body=content)
if response.status == 200:
print(f'The submission was successful. Google reported a {response.status} response code.')
else:
print(f'The submission was not successful. Google reported a {response.status} response code, instead of 200.')
我可以用它一个一个地添加 URL,但我想给它一个 CSV
文件,这样它会逐行读取它并将它发送到 Google。
我创建了以下代码:
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import csv
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 = "/content/astute-tractor-329613-1baed60ec1c0.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
with open('/content/indekseerida-1.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
content = """{
"url": row[0],
"type": "URL_UPDATED"
}"""
response, content = http.request(ENDPOINT, method="POST", body=content)
if response.status == 200:
print(f'The submission was successful. Google reported a {response.status} response code.')
else:
print(f'The submission was not successful. Google reported a {response.status} response code, instead of 200.')
这只返回错误响应代码 400。我对编码还很陌生,所以不要对我太苛刻 :)
在你的 for 循环中放入一个 print(content)
语句,我想你会
发现它不包含您认为它包含的内容。整个字符串只是您在此处键入的字面内容。
你需要这样的东西:
content = f"""{{
"url": {row[0]},
"type": "URL_UPDATED"
}}"""
f"""
创建一个字符串插值,在其中评估 {}
中的内容。然后你需要 {{
和 }}
来获得文字大括号。
我有一个 Google 索引 API 有效的代码。代码如下:
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 = "/content/astute-tractor-329613-1baed60ec1c0.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
content = """{
"url": "https://sitename.com/index.php",
"type": "URL_UPDATED"
}"""
response, content = http.request(ENDPOINT, method="POST", body=content)
if response.status == 200:
print(f'The submission was successful. Google reported a {response.status} response code.')
else:
print(f'The submission was not successful. Google reported a {response.status} response code, instead of 200.')
我可以用它一个一个地添加 URL,但我想给它一个 CSV
文件,这样它会逐行读取它并将它发送到 Google。
我创建了以下代码:
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import csv
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 = "/content/astute-tractor-329613-1baed60ec1c0.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
with open('/content/indekseerida-1.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
content = """{
"url": row[0],
"type": "URL_UPDATED"
}"""
response, content = http.request(ENDPOINT, method="POST", body=content)
if response.status == 200:
print(f'The submission was successful. Google reported a {response.status} response code.')
else:
print(f'The submission was not successful. Google reported a {response.status} response code, instead of 200.')
这只返回错误响应代码 400。我对编码还很陌生,所以不要对我太苛刻 :)
在你的 for 循环中放入一个 print(content)
语句,我想你会
发现它不包含您认为它包含的内容。整个字符串只是您在此处键入的字面内容。
你需要这样的东西:
content = f"""{{
"url": {row[0]},
"type": "URL_UPDATED"
}}"""
f"""
创建一个字符串插值,在其中评估 {}
中的内容。然后你需要 {{
和 }}
来获得文字大括号。