前言
百度目前提供自动提交链接和手动提交链接两种方式,其中自动提交又分为主动推送、自动推送和sitemap三种形式,按百度的说法,主动推送的效果最好,百度站长平台后台也提供了curl、php、ruby的推送示例代码但没有提供python代码,网上很少有现成的python版本主动推送代码但是我觉得过于复杂,而且只能主动提交一个网站的地址,现将目前我自己写的主动推送python代码贴出。
代码
# coding:utf8
import requests
def pushurls(url,site):
# 接口调用地址 在站长平台获取
urls = "http://data.zz.baidu.com/urls?site=" + site + "&token=" + token + ""
# urls.txt为需要推送的URL文件,每行一个
r = requests.post(urls, data=url)
baiduresult = u"推送"+site+",结果为:" + r.text + " "
print(baiduresult)
if __name__ == "__main__":
token = "XXXXXXXXX" # 你的token值
# urlsMap为二维数组,其中子数组中[0]为地址,[1]为网站
urlsMap = [['daohang.hellosmile.xin/index.php', 'daohang.hellosmile.xin'],
['darker.hellosmile.xin/index.php', 'darker.hellosmile.xin'],
['love.hellosmile.xin/index.html','love.hellosmile.xin'],
['about.hellosmile.xin/index.html','about.hellosmile.xin'],
['yq.hellosmile.xin/index.html','yq.hellosmile.xin'],
['yoyo.hellosmile.xin/index.html','yoyo.hellosmile.xin'],
['www.zcgjqb.cn/index.html','www.zcgjqb.cn']]
for j in range(1,4):
print("+++++++++++第" + str(j) + "次推送+++++++++++++")
for i in urlsMap:
pushurls(i[0], i[1])
运行效果
脚本每运行一次,将自动推送3次,为什么是3次呢,因为我是打算1分钟运行一次脚本,那么一天将会推送 2160次。