Python script for Bulk Submit URLs to Google Search Console

Python Submit Website URLs To Google Search Index

Today in this article, we will learn how to use Python script for Bulk Submit Website URLs to Google Search Console for Indexing or Crawling purpose.

We already learned how to set up the service account, project, and other configurations so that we can leverage Google’s Indexing API before.

Today in this article, we will cover below aspects,

If you have not performed mandatory prerequisites, please follow the below article for more details,

You may find the need to submit the URLs due to many reasons like article update due to new data or upgrade or article enhancement, etc.

In such scenarios, it is always difficult to perform indexing manually.

We shall be using below endpoints required for publishing the URLs,

SCOPES = [ “https://www.googleapis.com/auth/indexing ]

ENDPOINT = “https://indexing.googleapis.com/v3/urlNotifications:publish

In your python script please add below “using” namespace,

from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import json

Sample Script

from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import json

SCOPES= [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT= "https://indexing.googleapis.com/v3/urlNotifications:publish"

# thecodebuzz-da659b2b8b0d.json is the private key that you created for your service account.


JSON_KEY_FILE = "thecodebuzz-da659b2b8b0d.json" #Specify your service account JSON here
credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
url="url-1"
content = {
      "url": url,
      "type": "URL_UPDATED"
    }
    response, content = http.request(ENDPOINT, method="POST", body=json.dumps(content))
    print(response)

from_json_keyfile_name method uses the JSON file service account information which we created in the previous article.

The above code works perfectly fine for one URL which we are providing the variable “URL”

Please install packages like oauth2client or httplib2 as required.

Python script for Bulk Submit URLs to Google Search Console

You can use Python built-in function called enumerate to submit the URL list as below,

urlList=["url1","url2", "url3"]
for index, item in enumerate(urlList):
    content = {
      "url": item,
      "type": "URL_UPDATED"
    }
    response, content = http.request(ENDPOINT, method="POST", body=json.dumps(content))
    print(response)

Once successfully submitted the URLs for indexing, you can see the status code as 200 for the request.

Python script for Bulk Submit URLs to Google Search Console

References :

Please follow the below article for more details on prerequisites,

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *