教程:设置 Google News Publisher Center 与新闻 Sitemap

设置 Google News Publisher Center 并实现动态新闻 sitemap,确保你的文章能够被快速发现和收录。

前置条件

第 1 步:准备你的网站

内容要求

在申请 Google News 之前:

技术要求

第 2 步:申请 Google News Publisher Center

  1. 前往 news.google.com/publisher-center
  2. 点击 "Add Publication"
  3. 输入你的出版物名称
  4. 验证网站所有权(通过 Search Console)
  5. 配置你的出版物设置:
    • 出版物名称
    • 语言
    • 地区
    • 网站 URL

栏目配置

创建与你的内容分类相匹配的栏目:

Sections:
├── Technology
│   ├── AI & Machine Learning
│   ├── Software & Apps
│   └── Gadgets
├── Business
│   ├── Markets
│   └── Startups
└── Science
    ├── Space
    └── Environment

第 3 步:实现新闻 sitemap

服务端 sitemap 生成

from datetime import datetime, timedelta
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom

def generate_news_sitemap(articles):
    urlset = Element('urlset')
    urlset.set('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')
    urlset.set('xmlns:news', 'http://www.google.com/schemas/sitemap-news/0.9')

    cutoff = datetime.now() - timedelta(hours=48)

    for article in articles:
        if article['published'] < cutoff:
            continue

        url = SubElement(urlset, 'url')
        loc = SubElement(url, 'loc')
        loc.text = article['url']

        news = SubElement(url, 'news:news')

        publication = SubElement(news, 'news:publication')
        name = SubElement(publication, 'news:name')
        name.text = 'Example News'
        lang = SubElement(publication, 'news:language')
        lang.text = 'en'

        pub_date = SubElement(news, 'news:publication_date')
        pub_date.text = article['published'].isoformat()

        title = SubElement(news, 'news:title')
        title.text = article['title']

    xml_str = tostring(urlset, encoding='unicode')
    pretty_xml = minidom.parseString(xml_str).toprettyxml(indent="  ")
    return pretty_xml

用于定期更新的 Cron 任务

# Update news sitemap every 15 minutes
*/15 * * * * /usr/bin/python3 /path/to/generate_news_sitemap.py

第 4 步:配置 Indexing API

设置服务账号

  1. 前往 Google Cloud Console
  2. 创建一个新项目
  3. 启用 Indexing API
  4. 创建服务账号凭据
  5. 下载 JSON 密钥文件
  6. 将服务账号添加为 Search Console 所有者

实现

from google.oauth2 import service_account
from googleapiclient.discovery import build

def submit_url_to_indexing_api(url, key_file_path):
    credentials = service_account.Credentials.from_service_account_file(
        key_file_path,
        scopes=['https://www.googleapis.com/auth/indexing']
    )
    service = build('indexing', 'v3', credentials=credentials)

    request = service.urlNotifications().publish(
        body={
            'url': url,
            'type': 'URL_UPDATED'
        }
    )
    response = request.execute()
    return response

# Submit after publishing
submit_url_to_indexing_api(
    'https://example.com/2025/06/15/new-article',
    '/path/to/credentials.json'
)

第 5 步:监控效果

Search Console 新闻报告

定期查看以下报告:

自动化监控

def check_indexing_status(url, credentials):
    service = build('indexing', 'v3', credentials=credentials)
    response = service.urlNotifications().getMetadata(url=url).execute()
    return response.get('latestUpdate', {}).get('notifyTime', 'Not indexed')

验证清单