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

推荐订阅源

Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
D
Docker
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
The Cloudflare Blog
雷峰网
雷峰网
A
About on SuperTechFans
小众软件
小众软件
博客园 - Franky
博客园 - 聂微东
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
Engineering at Meta
Engineering at Meta
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
量子位
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
罗磊的独立博客
Martin Fowler
Martin Fowler
D
DataBreaches.Net
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Secure Thoughts
Project Zero
Project Zero
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
Security Latest
Security Latest
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks

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客户端的安装和使用方法 券商费率调整记 迷你主机组装记 Labwc更换定制皮肤 树莓派跑分流代理 实用自动代理配置示例 九年老鼠标复活记 Openvpn示例 Mac下如何旋转webp图片 如何隐藏Vscode中Markdown PDF插件的头部内容 解决Linux下网络配置无法变更保存的问题 labwc环境启用wlogout Ubuntu下新官方微信尝鲜 简单检测用户是否开启广告屏蔽 Atuin ZFS下卡顿问题解决 Wine安装64位微信 Fish 和 Atuin使用记 Fastapi简单实现临时下载链接 Fastapi微信公众号开发简要 Hugo根据语种展示不同内容 如何修复Waybar微信图标错误 信封加密简要 Linux环境下维护公众号记录 快速隐藏和呼出终端 Wayland环境自动切换壁纸 SuperTuxKart 试玩 简单抓取雪球股票快讯 为什么你的kill命令不能列出信号表 Wine安装微信保姆教程 Hysteria科学上网简要 Ubuntu 23.10 钉钉无法启动解决方法 网站导航栏防止插入Adsense自动广告 Linux环境按键检测 Hugo文末添加最后修改时间 Wayland环境ksnip无法复制问题解决 我眼中的股票市场 Caddy简单图片防盗链 greetd和regreet使用教程 Wayland环境下截图加后期修改 Wayland 环境下gif录屏 Firefox的一些有趣功能 Ubuntu 切换系统语言 Firefox 标题栏高度调整 网站添加回到顶部功能 Hugo自定义字体 Labwc 便捷配置 Wofi使用教程 窗口管理器labwc使用记 文件共享软件Dufs推荐 Hugo静态站点接入Adsense广告 Fcitx5配置详解 Ubuntu安装chrome的方法总结 一种Xterm.js的全屏方案 手机听离线音乐 Snipe it资产导入 应用启动器yofi使用配置 Snipe it资产管理系统安装使用 Ubuntu开机启动加速 Vue3 vite TypeScript跨域等相关设置 Ubuntu手动升级Libreoffice Ubuntu设置alacritty为默认终端 简体文章批量转换为繁体 使用Inkscape调整svg图片大小 实用命令 使用mitmproxy给手机app抓包 Python中rstrip方法细解 Github同主机多仓库部署deploy key问题处理 Linux安装最新版本Python KeePassXC使用教程 Ubuntu 22.10连接蓝牙耳机报错br-connection-profile-unavailable解决方法 Caddy日志配置轮转和格式化
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 会逐步处理,比百度快多了。