Python Script: Submit URL's to IndexNow API

LondoN eXtream

Regular Member
Joined
May 14, 2020
Messages
296
Reaction score
195
Python:
import requests
from bs4 import BeautifulSoup

# Configuration
SITEMAP_URL = "Your Sitemap URL"
YOUR_DOMAIN = "Your Domain"
INDEXNOW_KEY = "Your UUID Key"

INDEXNOW_ENDPOINT = "https://api.indexnow.org/indexnow"

def fetch_urls_from_sitemap(sitemap_url):
    response = requests.get(sitemap_url)
    response.raise_for_status()
    
    soup = BeautifulSoup(response.text, "xml")
    return [loc.text for loc in soup.find_all("loc")]

def notify_indexnow_single(url):
    payload = {
        "host": YOUR_DOMAIN.replace("https://", "").replace("http://", ""),
        "key": INDEXNOW_KEY,
        "keyLocation": f"{YOUR_DOMAIN}/{INDEXNOW_KEY}.txt",
        "urlList": [url]
    }

    response = requests.post(INDEXNOW_ENDPOINT, json=payload)
    return response.status_code == 200

def main():
    try:
        print(f"Fetching URLs from sitemap: {SITEMAP_URL}")
        urls = fetch_urls_from_sitemap(SITEMAP_URL)
        print(f"Found {len(urls)} URLs")

        if not urls:
            print("No URLs found.")
            return

        success_count = 0
        for i, url in enumerate(urls, 1):
            status = "Done" if notify_indexnow_single(url) else "Fail"
            if status == "Done":
                success_count += 1

            print("---------------------")
            print(f"URL Number: {i}")
            print(f"URL: {url}")
            print(f"Status: {status}")
            print(f"Total URL submitted: {success_count}")
            print("---------------------")
    except Exception as e:
        print("Error:", e)

if __name__ == "__main__":
    main()

Just edit with your sitemap url, domain and uuid key. The script will extract all url's from the sitemap and automatically send them to the index now API.
If this helped you, do not forget to like the post. Cheers.
 
Is there any method or script through which we send URL's of our Backlinks (3rd Party URL's )
 
Back
Top