首页
/ 使用searchConsoleR包调用Google URL Inspection API进行网页索引检查

使用searchConsoleR包调用Google URL Inspection API进行网页索引检查

2025-07-03 11:02:08作者:沈韬淼Beryl

概述

Google Search Console是网站管理员监控网站在Google搜索中表现的重要工具。其中URL Inspection API允许开发者以编程方式检查特定URL在Google索引中的状态。searchConsoleR包作为R语言接口,使得R用户能够方便地调用这一功能。

URL Inspection API简介

URL Inspection API提供以下关键信息:

  • 索引状态:URL是否被Google索引
  • 爬取状态:最近一次爬取时间
  • 移动设备可用性:是否符合移动友好标准
  • robots.txt状态:是否被阻止爬取
  • 规范URL:Google认定的规范版本
  • 引用来源:链接到该URL的其他页面

准备工作

安装与认证

首先需要安装并加载searchConsoleR包:

install.packages("searchConsoleR")
library(searchConsoleR)

然后进行认证,使用具有网站访问权限的Google账户:

scr_auth()

获取网站列表

查看你有权限访问的网站列表:

websites <- list_websites()
print(websites)

输出示例:

                                           siteUrl permissionLevel
1                   https://example.website.com/      siteFullUser
2                  sc-domain:code.markedmondson.me       siteOwner

基本使用方法

单URL检查

使用inspection()函数检查特定URL:

result <- inspection(
  url = "https://example.com/page-to-check",
  siteUrl = "https://example.com/"
)

结果解析

检查结果包含多个部分:

print(result)

典型输出结构:

==SearchConsoleInspectionResult==
===indexStatusResult===
$verdict: "PASS"
$coverageState: "Indexed, not submitted in sitemap"
$robotsTxtState: "ALLOWED"
$indexingState: "INDEXING_ALLOWED"
$lastCrawlTime: "2022-01-24 22:24:14 UTC"
$pageFetchState: "SUCCESSFUL"
$googleCanonical: "https://example.com/page-to-check"
$referringUrls: "https://www.example.com/referring-page/"

===MobileUsabilityResult===
$verdict: "PASS"

高级应用

批量检查多个URL

结合搜索分析数据,批量检查表现最佳的页面:

# 获取搜索表现最佳的页面
top_pages <- search_analytics(
  siteUrl = "https://example.com/",
  dimensions = "page",
  rowLimit = 50
)

# 批量检查前10个页面
top_urls <- head(top_pages$page, 10)
results <- lapply(top_urls, function(url) {
  inspection(url, siteUrl = "https://example.com/")
})

并行处理加速

使用future.apply包实现并行处理:

library(future.apply)
plan(multisession)  # 设置并行计划

# 自定义检查函数
check_url <- function(url, site) {
  scr_auth()  # 每个并行会话需要单独认证
  inspection(url, siteUrl = site)
}

# 并行执行检查
parallel_results <- future_lapply(
  top_urls, 
  check_url, 
  site = "https://example.com/"
)

配额管理与优化

API限制

URL Inspection API有以下限制:

  • 每日2000次查询
  • 每分钟600次查询

使用自有客户端ID

为避免共享默认配额,建议使用自有客户端ID:

googleAuthR::gar_set_client("path/to/your-client-id.json")

服务账户认证

对于生产环境,推荐使用服务账户:

scr_auth(json = "path/to/service-account-key.json")

结果分析与可视化

提取关键指标

# 提取所有URL的最后爬取时间
crawl_times <- sapply(results, function(x) {
  x$indexStatusResult$lastCrawlTime
})

# 转换为数据框便于分析
status_df <- data.frame(
  url = top_urls,
  last_crawled = as.POSIXct(crawl_times, origin = "1970-01-01"),
  indexed = sapply(results, function(x) x$indexStatusResult$verdict == "PASS"),
  mobile_friendly = sapply(results, function(x) x$mobileUsabilityResult$verdict == "PASS")
)

可视化检查结果

library(ggplot2)

ggplot(status_df, aes(x = last_crawled, y = url, color = indexed)) +
  geom_point(size = 3) +
  labs(title = "URL索引状态检查", 
       x = "最后爬取时间", 
       y = "URL") +
  theme_minimal()

最佳实践建议

  1. 定期检查关键页面:为重要页面设置定期检查机制
  2. 监控索引问题:重点关注返回"FAIL"状态的URL
  3. 优化爬取频率:分析最后爬取时间,识别更新频繁但爬取不及时的页面
  4. 移动优先:确保所有页面通过移动设备可用性检查
  5. 配额管理:对于大型网站,合理安排检查频率避免超出配额

通过searchConsoleR包,R用户可以高效地将Google Search Console的URL检查功能集成到数据分析流程中,实现网站SEO表现的自动化监控与优化。

登录后查看全文
热门项目推荐

项目优选

收起
kernelkernel
deepin linux kernel
C
27
11
docsdocs
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
472
3.49 K
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
10
1
leetcodeleetcode
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
65
19
flutter_flutterflutter_flutter
暂无简介
Dart
719
173
giteagitea
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
23
0
kernelkernel
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
213
86
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.27 K
696
rainbondrainbond
无需学习 Kubernetes 的容器平台,在 Kubernetes 上构建、部署、组装和管理应用,无需 K8s 专业知识,全流程图形化管理
Go
15
1
apintoapinto
基于golang开发的网关。具有各种插件,可以自行扩展,即插即用。此外,它可以快速帮助企业管理API服务,提高API服务的稳定性和安全性。
Go
22
1