做網站的人一般都很關注網站的收錄量,而收錄量就需要蜘蛛去爬取,而爬取則需要去提交。
而百度提供了api提交的方法。
用Python/ target=_blank class=infotextkey>Python進行提交的話,我選擇的是post提交。
POST /urls?site=“自己網站的token” # 這里是site后面那一段帶網站的,[color=#f95d5d][font=tahoma, PingFangSC, "]site[/font][/color][color=#666666][font=tahoma, PingFangSC, "]=[/font][/color][color=#666666][font=tahoma, PingFangSC, "]https://zjzdmc.top[/font][/color][color=#666666][font=tahoma, PingFangSC, "]&[/font][/color][color=#f95d5d][font=tahoma, PingFangSC, "]token[/font][/color][color=#666666][font=tahoma, PingFangSC, "]=xxxxxxxx,并非是整個[/font][/color][color=#666666][font=tahoma, PingFangSC, "]接口調用地址。[/font][/color]
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Type: text/plain
Content-Length: 83
這個第一行的token請換成自己的。
私信小編01即可獲取大量Python學習資源
http://www.example.com/1.html
http://www.example.com/2.html
這個是提交示例,也就是說一個一行。
{
"remain":99998,
"success":2,
"not_same_site":[],
"not_valid":[]
}
這個是返回信息,那么如何去寫就一目了然了。
Python源碼:
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.htmlnhttps://xxxxx.htmlnhttps://xxxxx.htmlnhttps://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中呢,如果是則回答1n")
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("請將內容填充!5秒鐘后自動關閉")
time.sleep(5)
break
print('*'*30)
new_answer = input("是否還需要提交,如果是的話請先去更改一下相應文件,如果是請輸入1,如果否請輸入0n")
if new_answer == '0':
print("提交結束,5秒鐘后自動關閉")
time.sleep(5)
break
push_num += 1
print("現在開始第"+str(push_num)+'次提交')
需要在python文件新建兩個txt文件,分別放token和鏈接。
然后其他人就可以直接雙擊打開python文件進行提交了。如果有什么疑問可以直接問。