萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

Python xmlrpclib 實作 Ping 服務:主動通知 Google 與百度加速收錄

部落格發文後主動 Ping 搜尋引擎(Google、百度等)可大幅縮短收錄時間。利用 xmlrpclib 呼叫標準 weblogUpdates.extendedPing 介面,無需依賴 WordPress 等 CMS 內建功能,即可在本地腳本完成自動通知。

核心 Ping 函式

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')

批量處理 URL 清單

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}

擴充建議

  • 支援多 Ping 端點:rpc.pingomatic.com、blogsearch.google.com、ping.baidu.com
  • 加入重試與指數退避機制
  • 記錄 Ping 歷史避免重複通知
  • 配合 sitemap.xml 生成器實現全自動 SEO 流程
copyright © 萬盛學電腦網 all rights reserved