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

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

2025-07-03 14:16:21作者:沈韬淼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表现的自动化监控与优化。

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