功能描述 利用crontab定时执行py文件自动提交文章URL给微软bing
配置 新建一个urldelay.py
文件
文件内容
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 import requestsimport jsonimport xml.etree.ElementTree as ET sitemap_url = 'https://blog.5772447.xyz/sitemap.xml' response = requests.get(sitemap_url)if response.status_code == 200 : root = ET.fromstring(response.content) url_list = [] for url_element in root.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}url' ): if len (url_list) >= 10 : break url = url_element.findtext('{http://www.sitemaps.org/schemas/sitemap/0.9}loc' ) if url: url_list.append(url) print (url_list) data = { "siteUrl" : "https://blog.5772447.xyz" , "urlList" : url_list } api_key = "自己的微软站长推送api" headers = { "Content-Type" : "application/json; charset=utf-8" } submit_url = f"https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey={api_key} " response = requests.post(submit_url, json=data, headers=headers) if response.status_code == 200 : print ("URLs提交成功!" ) else : print ("URLs提交失败。状态码:" , response.status_code)else : print ('Failed to retrieve sitemap. Status code:' , response.status_code)
自行配置代码中的 sitemap_url = ‘https://blog.5772447.xyz/sitemap.xml ‘
“siteUrl”: “https://blog.5772447.xyz “,
api_key = “自己的微软站长推送api”
PS:自行安装 requests 库,可以使用以下命令:
如果Python环境是通过apt进行管理,的可以使用以下命令:
1 2 apt update apt install python3-requests
crontab使用配置 将上述文件存于文件夹/home/script
,具体地址可以自定义
使用命令
然后在最下面填写
1 0 9 * * * python3 /home/script/urldelay.py >> /home/script/urldelay.log 2>&1
最后ctrl+X 按y 回车保存
具体执行情况 可以在/home/script/urldelay.log
文件中查看
1 2 ['https://blog.5772447.xyz/tags/index.html', 'https://blog.5772447.xyz/categories/index.html', 'https://blog.5772447.xyz/posts/973246e2/', 'https://blog.5772447.xyz/posts/29dc6fe8/', 'https://blog.5772447.xyz/posts/29dc6fe9/', 'https://blog.5772447.xyz/posts/c817a32b/', 'https://blog.5772447.xyz/posts/7c4a1a51/', 'https://blog.5772447.xyz/posts/8373160e/', 'https://blog.5772447.xyz/posts/500de237/', 'https://blog.5772447.xyz/posts/c817a34b/'] URLs提交成功!