时间: 2023-05-24 【学无止境】 阅读量:共1283人围观
简介 API提交和手动提交共享配额,每日至多提交10万条有价值的内容,sitemap提交配额不与其他方式共享,具体配额以站点页面显示数据为准 。配额不可累计,当日有效。
配置config.json
{
"api": "",
"##": "mode:local or sitemap",
"mode": "",
"##": "如果是local就写本地路径,如果是sitemap就写链接",
"path": ""
}
其中,config.js相关参数说明:
"api":进入百度站长平台,查看推送接口
"mode":分为local和sitemap
local:读取本地txt文件中的链接进行推送
sitemap:或者网站sitemap网址中的链接进行推送,只支持xml格式
"path":
local模式:填本地地址,绝对或者相对都行
sitemap模式:填写sitemap的链接
推送代码实现
import json
import os.path
import re
import requests
def get_config():
file = "config.json"
if not os.path.exists(file):
with open(file,'w',encoding='utf8') as f:
txt = '{\n' \
'"api": "",\n' \
'"##": "mode:local or sitemap",\n' \
'"mode": "",\n' \
'"##": "如果是local就写本地路径,如果是sitemap就写链接",\n' \
'"path": ""\n' \
'}'
f.write(txt)
with open(file,'r',encoding='utf8') as f:
return json.load(f)
myconfig = get_config()
def push_baidu(url):
api = myconfig['api']
headers = {
"User-Agent": "curl/7.12.1",
"Host": "data.zz.baidu.com",
"Content-Type": "text/plain",
"Content-Length": "83"
}
respons = requests.post(url=api, data=url, headers=headers)
return respons.text
def get_urls_txt(file):
with open(file=file, mode='r', encoding='utf8') as f:
return f.read()
def get_urls_xml(url):
res_text = requests.get(url).text
urls = re.findall(r"<loc>(.+?)</loc>", res_text)
urls_txt = ""
for i in urls:
urls_txt += f"{i}\n"
return urls_txt
if __name__ == '__main__':
try:
mode = myconfig['mode']
if mode == 'local':
urls = get_urls_txt(myconfig['path'])
print(push_baidu(urls))
elif mode == 'sitemap':
urls = get_urls_xml(myconfig['path'])
print(push_baidu(urls))
else:
print("config.json配置错误,请检查!")
except Exception as e:
print("config.json配置错误,请检查!")