python从sitemap获更新主动推送链接至百度站长平台
source link: https://cjh0613.com/20200529BaiduUrlsPush.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
python从sitemap获更新主动推送链接至百度站长平台
百度站长平台对github.io上存放的sitemap.xml不太友好,经常抓取失败,添加sitemap有被降权的风险;近期自动推送js也停用了;那就只有手动提交或程序主动推送了。
网上已有hexo博客站点自动推送sitemap.xml中全部网页链接至百度的代码。但是每次提交没必要全部链接,而且重复提交页面没有变动的链接可能会被百度降权。因此我写了这个只推送有更新的页面链接的代码。只有10min内更新过的页面才会被推送。(可以修改)
另外,虽然已有hexo百度推送插件,但配置繁琐。这个代码也适用与其他能自动生成google格式sitemap的网站、博客。
这个代码针对google格式sitemap(使用ISO时间如2020-05-28T10:54:43.663Z
),安装hexo-generator-sitemap
后即可使用。
推送 sitemap 里,更新时间距现在600秒以内的网页链接。
可以在你的hexo一键部署脚本末尾添加运行此一键推送给百度收录程序的代码。
代码已传至github,欢迎改进PR.https://github.com/cjh0613/python-pub
import requests
import time
import datetime
import dateutil.parser
from bs4 import BeautifulSoup as bp
def get_(data):
headers={'User-Agent':'curl/7.12.1 ',
'Content-Type':'text/plain '}
try:
r = requests.post(url='你的百度主动推送地址',data=data)
print(r.status_code)
print(r.content)
except Exception.e:
print(e)
print('自动推送开启....','utf-8')
time.sleep(0.5)
site_url = '你的sitemap.xml地址'
try:
print('获取sitemap链接....','utf-8')
data_ = bp(requests.get(site_url).content,'lxml')
except Exception.e:
print(e)
list_url=[]
list_date=[]
print('---------------------------------')
for x,y in enumerate(data_.find_all('loc')):
print(x,y.string)
list_url.append(y.string)
for x2,y2 in enumerate(data_.find_all('lastmod')):
startTime=y2.string
startTime=dateutil.parser.parse(startTime)
date1=(startTime.isoformat())[0:10]
startTime=date1+" "+(startTime.isoformat())[11:19]
startTime=datetime.datetime.strptime(startTime,"%Y-%m-%d %H:%M:%S")
now=datetime.datetime.utcnow()
endTime = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second)
date2=(endTime.isoformat())[0:10]
date = endTime- startTime
seconds=date.seconds
if date1==date2 and seconds<600:#可修改,推送sitemap里,更新时间距现在600秒以内的网页链接
list_date.append(x2)
print('---------------------------------')
print(list_date)
print('开始推送....','utf-8')
for x in list_date:
cjhurl=list_url[x]
print('当前推送条目为:','utf-8' + cjhurl)
get_(cjhurl)
BTC地址(建议使用ETH):3F7hk94nnBHrL4LuHZdtP4soPCefCV6CbU
ETH收款地址:0x2AdDE524665af970acE4CB3D50dE70136c88f90F
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK