部落格發文後主動 Ping 搜尋引擎(Google、百度等)可大幅縮短收錄時間。利用 xmlrpclib 呼叫標準 weblogUpdates.extendedPing 介面,無需依賴 WordPress 等 CMS 內建功能,即可在本地腳本完成自動通知。
import xmlrpclib
def ping(webname, hosturl, linkurl):
rpc = xmlrpclib.ServerProxy('https://blogsearch.google.com/ping/RPC2')
result = rpc.weblogUpdates.extendedPing(webname, hosturl, linkurl)
print(result)
if result.get('flerror', False):
print('ping error')
else:
print('ping success')
import re
urlinfo = '''https://www.example.com/post/1.html
https://www.example.com/post/2.html'''
def get_url(url):
'''提取標準化 host URL(含 scheme + host + /)'''
m = re.search(r'^https?://(.*?)(/|$)', url, re.IGNORECASE)
return m.group(0) if m else url
for link in urlinfo.splitlines():
if not link.strip(): continue
webname = link.split('.')[1] # 簡易取域名中段
hosturl = get_url(link)
ping(webname, hosturl, link)
{'message': 'Thanks for the ping.', 'flerror': False}
rpc.pingomatic.com、blogsearch.google.com、ping.baidu.comsitemap.xml 生成器實現全自動 SEO 流程