惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
腾讯CDC
J
Java Code Geeks
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
雷峰网
雷峰网
B
Blog RSS Feed
博客园_首页
量子位
F
Fortinet All Blogs
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog

mephisto.cc

修复rime-ice无法弹出候选框的问题 开机滚动字体显示优化 个人网站安全防护 树莓派上部署Vaultwarden 使用goaccess实时分析Caddy日志 个人网站监控 Hugo全站AVIF记 Linux下尝试使用Godot开发小游戏 Arch linux dae 透明代理 Airflow接管galler-dl下载任务 如何使用gallery-dl批量下载图像 Arch核显下如何愉快玩Dota2和CS2 Arch/labwc 环境网络相关设置 Rime添加dota2词库 Arch 重装记录 Supertuxkart iOS版终于发布了 打造自己的流媒体音乐服务 剪切板管理工具clipcat推荐 MangoHud性能监控 微信小程序开发记 Rofi试用 解决Arch下VSCode无法输入中文的问题 独立窗口管理器下无法录屏问题处理 上海市二手房成交数据监控 OpenLDAP监控 Arch linux如何顺畅连接蓝牙设备 使用Git和Ansible管理配置文件 Arch Linux SSL VPN 客户端配置 简单获取celery任务实时结果 Arch linux下iNode客户端的安装和使用方法
Google网址收录api Python示例
2023-04-10 · via mephisto.cc

维护个人网站有一部分工作是让搜索引擎快速收录网页内容,获得自然搜索流量服务更多用户。直接去 Google 等搜索引擎提交是一种方式,使用 API 直接提交是另外一种方式,当你一次想提交多个网址的时候,API 的方式更加方便。

1. 创建谷歌云服务账户

Google index api 是和 google 云平台上管理的,这一步过程省略(网络上有很多相关教程),设置成功后会下载一个 json 文件存到本地,文件里面是一些密钥信息,通过这个密钥文件,才能获得授权调用 Google 相关的 api

google cloud acount setting

2. 编写 Python 程序调用 api

  • 准备环境,安装依赖包

    1pip install --upgrade google-api-python-client oauth2client
    
  • 文件预览

    1(.venv) ➜  google-api-python-client tree
    2.
    3├── google_batch.py
    4├── google_index.py
    5├── hugo-368210-30b9660ab8b3.json
    6├── readme.txt
    7└── urls.txt
    8
    90 directories, 5 files
    
    • google_batch.py 用于批量提交

      提交收录一般为URL_UPDATED,当然也可以删除,有需要可查看对应的官方文档。

      程序文件内容如下:

       1from oauth2client.service_account import ServiceAccountCredentials
       2from googleapiclient.discovery import build
       3from googleapiclient.http import BatchHttpRequest
       4import httplib2
       5
       6
       7with open("urls.txt") as f:
       8    new_urls = f.readlines()
       9
      10
      11JSON_KEY_FILE = "hugo-368210-30b9660ab8b3.json"
      12SCOPES = ["https://www.googleapis.com/auth/indexing"]
      13ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
      14
      15# Authorize credentials
      16credentials = ServiceAccountCredentials.from_json_keyfile_name(
      17    JSON_KEY_FILE, scopes=SCOPES
      18)
      19http = credentials.authorize(httplib2.Http())
      20
      21# Build service
      22service = build("indexing", "v3", credentials=credentials)
      23
      24
      25def insert_event(request_id, response, exception):
      26    if exception is not None:
      27        print(exception)
      28    else:
      29        print(response)
      30
      31
      32batch = service.new_batch_http_request(callback=insert_event)
      33
      34# url updated
      35for url in new_urls:
      36    batch.add(
      37        service.urlNotifications().publish(body={"url": url, "type": "URL_UPDATED"})
      38    )
      39
      40batch.execute()
      
    • google_index.py 单个网址提交

      文件里面的content部分就是你要提交的网址信息和操作类型。

      程序示例:

       1
       2from oauth2client.service_account import ServiceAccountCredentials
       3import httplib2
       4
       5SCOPES = ["https://www.googleapis.com/auth/indexing"]
       6ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
       7
       8# service_account_file.json is the private key that you created for your service account.
       9JSON_KEY_FILE = "hugo-368210-30b9660ab8b3.json"
      10
      11credentials = ServiceAccountCredentials.from_json_keyfile_name(
      12    JSON_KEY_FILE, scopes=SCOPES
      13)
      14
      15http = credentials.authorize(httplib2.Http())
      16
      17# Define contents here as a JSON string.
      18# This example shows a simple update request.
      19# Other types of requests are described in the next step.
      20
      21content = """{
      22\"url\": \"http://mephisto.cc/zh-tw/tech/s2t/\",
      23\"type\": \"URL_UPDATED\"
      24}"""
      25
      26response, content = http.request(ENDPOINT, method="POST", body=content)
      27print("response:", response)
      28print("content:", content)
      
    • hugo-368210-30b9660ab8b3.json 密钥文件

      这个是创建谷歌云服务帐户后下载到本地的,里面有敏感信息,value 部分已经手动删除了, 仅供参考。

      示例:

       1{
       2  "type": "service_account",
       3  "project_id": "",
       4  "private_key_id": "",
       5  "private_key": "",
       6  "client_email": "",
       7  "client_id": "",
       8  "auth_uri": "",
       9  "token_uri": "",
      10  "auth_provider_x509_cert_url": "",
      11  "client_x509_cert_url": ""
      12}
      
    • urls.txt 批量网址文件

      这个是我自己习惯的网址存放方式,一行一个网址,示例:

      1(.venv) ➜  google-api-python-client cat urls.txt
      2https://mephisto.cc//note/hero-chen/
      3https://mephisto.cc/zh-tw/note/hero-chen/
      

      上面就是一个文章的简体地址和繁体地址,单文件更新的时候不需要这个文件,也可以改成自己需要组织格式,懂 python 的自己修改下 google_batch.py文件即可。

3. 多网址提交实际操作示例

  • 修改urls.txt,这一步可以自己通过程序自动生成

  • 运行程序

  • 示例

    1(.venv) ➜  google-api-python-client python google_batch.py
    2{'urlNotificationMetadata': {'url': 'https://mephisto.cc/tech/google-index-api-python/\n', 'latestUpdate': {'url': 'https://mephisto.cc/tech/google-index-api-python/\n', 'type': 'URL_UPDATED', 'notifyTime': '2023-04-10T01:48:20.759700010Z'}}}
    3{'urlNotificationMetadata': {'url': 'https://mephisto.cc/zh-tw/tech/google-index-api-python/\n', 'latestUpdate': {'url': 'https://mephisto.cc/zh-tw/tech/google-index-api-python/\n', 'type': 'URL_UPDATED', 'notifyTime': '2023-04-10T01:48:20.759685330Z'}}}
    

单文件提交就不操作了,当你提交收录请求后,Google 会逐步处理,比百度快多了。