Created
May 27, 2019 04:49
-
-
Save alucard001/fedce21b80a3de7aa3c07299838e1462 to your computer and use it in GitHub Desktop.
Google Indexing API V3 Working example with Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from oauth2client.service_account import ServiceAccountCredentials | |
import httplib2 | |
import json | |
import pandas as pd | |
# https://developers.google.com/search/apis/indexing-api/v3/prereqs#header_2 | |
JSON_KEY_FILE = "json_key_file_downloaded_after_creating_your_google_service_account_see_above_details_on_how_to_do.json" | |
SCOPES = ["https://www.googleapis.com/auth/indexing"] | |
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES) | |
http = credentials.authorize(httplib2.Http()) | |
def indexURL(urls, http): | |
# print(type(url)); print("URL: {}".format(url));return; | |
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish" | |
for u in urls: | |
# print("U: {} type: {}".format(u, type(u))) | |
content = {} | |
content['url'] = u.strip() | |
content['type'] = "URL_UPDATED" | |
json_ctn = json.dumps(content) | |
# print(json_ctn);return | |
response, content = http.request(ENDPOINT, method="POST", body=json_ctn) | |
result = json.loads(content.decode()) | |
# For debug purpose only | |
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"])) | |
""" | |
data.csv has 2 columns: URL and date. | |
I just need the URL column. | |
""" | |
csv = pd.read_csv("my_data.csv") | |
csv[["URL"]].apply(lambda x: indexURL(x, http)) |
I see that a lot of people have some issues with this code. I've created a code for checking index status of your URLs and submitting non-indexed ones to Google Search. please consider to use it = https://github.com/rus779/GSearch-Index-Submit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I solved this problem after a lot of trial and error.
If you need help with this script, please contact me on telegram.
https://t.me/ordmoong