进入必应站长:https://www.bing.com/webmasters/indexnow
右上角 设置—>API访问—>查看API密钥
Python 脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import requests import re import json
apiKey = '上面获取的apiKey'
siteUrl = 'https://blog.365day.top/'
limit = 20
sitemapUrl = 'https://blog.365day.top/sitemap.xml'
def get_urls(sitemap): response = requests.get(url=sitemap) urls = re.findall('<loc>(.*?)</loc>', response.text) urls.sort(reverse=True) return urls
def submit_bing(site_url: str, url_list: list, api_key: str): try: response = requests.post(url=f"https://ssl.bing.com//webmaster/api.svc/json/SubmitUrlbatch?apikey={api_key}", json={ 'siteUrl': site_url, 'urlList': url_list }, headers={ 'Host': 'ssl.bing.com', 'Content-Type': 'application/json; charset=utf-8', }) except Exception as e: print(e) finally: if response.status_code == 200 and response.json().get('d') is None: print('推送成功:') for i in url_list: print(i) get_Quota(site_url,api_key) else: print('推送失败:推送配额不足!') get_Quota(site_url,api_key)
def get_Quota(site_url: str, api_key: str): try: response = requests.get(url=f"https://ssl.bing.com//webmaster/api.svc/json/GetUrlSubmissionQuota?siteUrl={site_url}&apikey={api_key}", headers={ 'Host': 'ssl.bing.com', 'Content-Type': 'application/json; charset=utf-8', }) result = json.loads(response.text) DailyQuota = result.get('d').get('DailyQuota') MonthlyQuota = result.get('d').get('MonthlyQuota') print("今日剩余可推送:" , DailyQuota , "条") print("本月剩余可推送:" , MonthlyQuota , "条") except Exception as e: print(e)
if __name__ == '__main__': urlList = get_urls(sitemapUrl)[-limit:] submit_bing(site_url=siteUrl, url_list=urlList, api_key=apiKey)
|
使用 Python 和站点的 sitemap ,将站点 URL 推送到必应站长