之前一直用的一个关于百度 API 提交的工具貌似不好用了,所以准备自己写一个。
其实感觉不好用可能是因为我的站点信息里看提交都是0。但大概率是百度自己出了问题,既然如此,那么还是重新写一个工具吧。
我们可以看百度自己的教程和示例:
post 推送示例
POST /urls site=www.58.com&token=edk7ychrEZP9pDQD HTTP/1.1
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Length: 83
http://www.example.com/1.html
http://www.example.com/2.html
我们仿照一下写一个 headers 即可。
1 2 3 4 5
| headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36,', 'Host': 'data.zz.baidu.com', 'Content-Length': '83' }
|
然后创建一个函数来进行判断是否有链接和 API 导入,当然我这次写的是可以直接用的,只需要创建一个 api.txt
然后把 API 放进去,创建一个 links.txt
把链接放进去,每行一个链接即可。
本次主要使用了三个库,分别是 requests
,re
,time
。它们分别用于提交、提取以及等待。
具体的效果还是自己测试吧,我把代码放在这里一份,另一份放在我的 GitHub 上:
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
| import requests import re import time
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36,', 'Host': 'data.zz.baidu.com', 'Content-Length': '83' }
print("*"*30) print('links.txt 示例:\nhttps://xxxxx.html\nhttps://xxxxx.html\nhttps://xxxxx.html\nhttps://xxxxx.html') print("*"*30) print('api 示例:\nhttp://data.zz.baidu.com/urls?site=xxxxxxxxxxx')
push_num = 1
while push_num < 9999: if push_num == 1: answer = input("请问你是否已经将链接填入 links.txt,api填入 api.txt 中呢,如果是则回答1\n") if answer == '1': try: with open('links.txt', 'r') as links: links = links.read() except FileNotFoundError: print("links.txt 文件不存在") try: with open('api.txt', 'r') as api: api = api.read() except FileNotFoundError: print("links.txt 文件不存在")
def thinklink(links, api): if links == '': print("links.txt 文件为空") else: if api == '': print('api.txt 为空') else: try: html_result = requests.post(api, headers=headers, timeout=5, data=links).text return html_result except: return print("失败")
push_result = thinklink(links, api) print('提交完成:' + push_result)
surplus_push_num = re.findall('"remain":(.*),"', push_result) surplus_push_num = surplus_push_num.pop() print('剩余' + surplus_push_num + '次提交机会') else: print("请将内容填充!")
time.sleep(5) break
print('*'*30) new_answer = input("是否还需要提交,如果是的话请先去更改一下相应文件,如果是请输入1,如果否请输入0\n") if new_answer == '0': print("提交结束") time.sleep(5) break
push_num += 1 print("现在开始第" + str(push_num) + '次提交')
|
如果有任何问题或疑问,可以在评论区留言,我会尽快回复。