你问我答:使用主动推送功能会达到怎样效果?
及时发现:可以缩短百度爬虫发现您站点新链接的时间,使新发布的页面可以在第一时间被百度收录
保护原创:对于网站的最新原创内容,使用主动推送功能可以快速通知到百度,使内容可以在转发之前被百度发现
脚本功能,从sitemap获取url,并实现主动推送功能,不会sitemap制作点python百万sitemap制作
posturl.py
#-*- coding: utf-8 -*-
import requests
import re
__Author__ = "Kxrr" #原作者,github上看到有有懒得自己写了,懒癌晚期
__Verson__ = 0.1
class Pbs(object):
def __init__(self, urlSitemap, urlPost):
self.urlSitemap = urlSitemap
self.urlPost = urlPost
self.HEADERS_POST = {
"User-Agent": "curl/7.12.1",
"Host": "data.zz.baidu.com",
"Content-Type": "text/plain",
"Content-Length": "83"
}
self.f = open("_urlPosted.txt", "a+")
self.fContent = self.f.read()
self.f.close()
def getSitemap(self):
self.responseSitemap = requests.get(self.urlSitemap)
self.listSitemap = re.findall("(.*?)", self.responseSitemap.content, re.S)
def sortList(self):
self.g = open("_urlPosted.txt", "a+")
def postSitemap(self):
for self.eachUrl in self.listSitemap:
if self.eachUrl in self.fContent:
pass
else:
self.responsePost = requests.post(self.urlPost, data=self.eachUrl, headers=self.HEADERS_POST)
if ""success":1" in self.responsePost.content:
self.g.write(self.eachUrl + " ")
print self.eachUrl + " Done."
self.g.close()
def run(self):
self.getSitemap()
self.sortList()
self.postSitemap()
main.py
#-*- coding: utf-8 -*-
from PostBaiduSitemap import Pbs
urlSitemap = "" # Your sitemap url, like "https://blog.kxrr.us/index.php/sitemap"
urlPost = "" # Your Baidu API, like "https://data.zz.baidu.com/urls?site=blog.kxrr.us&token=xxxxxxxxxxxx"
if __name__ == "__main__":
pbs = Pbs(urlSitemap,urlPost)
pbs.run()
使用方法
在 main.py 中的
urlSitemap 填写你的Sitemap地址,形如 "https://blog.kxrr.us/index.php/sitemap’
urlPost 填写你的百度接口地址, 形如 "https://data.zz.baidu.com/urls?site=blog.kxrr.us&token=xxxxxxxxxxxx’
需要安装 requests 模块, pip install requests。
运行 python main.py。 可以在VPS中设置定时任务。
实现功能
主动推送,目前为分条推送。
一条URL推送成功后,将保存在 _urlPosted.txt 中,第二次运行将不再推送。