新网站在在部署完成之后,为了加快新网站在bing上的索引速度,我们通过调用Bing IndexNow的api将新网站的相关url进行提交。

1 Bind IndexNow API调用官方教程

官方教程地址:https://www.bing.com/indexnow/getstarted#implementation

主要总结为以下四步:
1. 生成api key
2. 下载api key的txt文件,并将这个文件放到你的网站根目录下,可以通过 https://你的网站域名/api key uuid.txt 访问到(比如https://www.example.com/50498a594fc84600966dc00951839f5e.txt ),这一步非常重要
3. 向bing indexnow接口发送post请求,在请求体中加入需要索引的新网站urls
4. 然后进入你网站管理后台Bing Webmaster Tools,在IndexNow tab页查看是否成功提交了urls

成功提交之后会显示你通过代码提交的相关网址,如下图所示

独立开发 – 通过Bing IndexNow API提交网址到bing-StubbornHuang Blog

2 调用Bind IndexNow API提交新网站链接

我用python简单实现了如何调用bing indexnow接口发送post请求的代码,示例代码如下

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json


def submit_urls_to_indexnow(host, api_key, key_location, url_list):
    """
    向IndexNow API提交URL列表

    Args:
        host: 网站主机名 (例如: www.example.org)
        api_key: IndexNow API密钥
        key_location: API密钥文件的完整URL
        url_list: 要提交的URL列表

    Returns:
        提交结果
    """
    # API端点
    api_url = "https://api.indexnow.org/IndexNow"

    # 请求头
    headers = {
        "Content-Type": "application/json; charset=utf-8"
    }

    # 请求数据
    payload = {
        "host": host,
        "key": api_key,
        "keyLocation": key_location,
        "urlList": url_list
    }

    try:
        print(f"正在提交 {len(url_list)} 个URL到IndexNow...")
        print(f"目标主机: {host}")

        # 发送POST请求
        response = requests.post(api_url, headers=headers, json=payload, timeout=30)

        print(f"响应状态码: {response.status_code}")

        if response.status_code == 200:
            print("URL提交成功!")
            return {
                "success": True,
                "status_code": response.status_code,
                "message": "提交成功"
            }
        else:
            print(f"提交失败,状态码: {response.status_code}")
            print(f"响应内容: {response.text}")
            return {
                "success": False,
                "status_code": response.status_code,
                "error": response.text
            }

    except requests.exceptions.RequestException as e:
        print(f"网络请求错误: {e}")
        return {
            "success": False,
            "error": str(e)
        }
    except Exception as e:
        print(f"未知错误: {e}")
        return {
            "success": False,
            "error": str(e)
        }


if __name__ == "__main__":
    host = "www.example.com"
    api_key = "50498a594fc84600966dc00951839f6666e"
    key_location = f"https://{host}/{api_key}.txt"

    url_list = [
        "https://www.example.com",
        "https://www.example.com/a" # 其他网址
    ]

    result = submit_urls_to_indexnow(host, api_key, key_location, url_list)
    print(json.dumps(result, ensure_ascii=False, indent=2))

将上述代码中的host和api_key修改为你自己的网站地址和api key,并且将api key的txt文件上传到网站根目录并可以通过浏览器访问,之后运行上述代码即可成功调用Bing IndexNow接口提交新网站网址,加快新网站在bing上的索引速度,优化seo。