使用R语言scholar包提取Google Scholar引用及索引信息报错求助
修复R的scholar包提取Google Scholar引用数据的错误
问题说明
运行提供的代码时出现错误:Error in scholar(query, pages = 1) : is.character(id) is not TRUE,核心原因是对scholar包的函数用法存在误解。
错误原因
scholar()函数要求传入的是Google Scholar作者ID(即作者个人主页URL中user=后的字符串),而非姓名+学校的搜索关键词。直接传入关键词会触发类型校验错误。
修复方案
1. 获取作者的Google Scholar ID
打开Google Scholar,搜索目标作者并进入其个人主页,从URL中提取user=后的ID。例如:
- 若John Smith的主页URL为
https://scholar.google.com/citations?user=abc123,则对应的ID为abc123。
2. 修正代码
使用get_profile()函数(scholar包中专门用于获取作者引用数据的函数),结合作者ID提取所需信息:
library(scholar) # 补充作者的Google Scholar ID professors <- data.frame( name = c("John Smith", "Jane Doe", "Mike Johnson"), school = c("Harvard University", "Stanford University", "MIT"), scholar_id = c("替换为实际ID", "替换为实际ID", "替换为实际ID") # 填入你获取到的真实ID ) get_citation_info <- function(name, school, scholar_id) { # 获取作者完整档案 profile <- get_profile(scholar_id) # 提取所需指标 total_cites <- profile$total_cites h_index <- profile$h_index i10_index <- profile$i10_index return(data.frame( name = name, school = school, total_citations = total_cites, h_index = h_index, i10_index = i10_index )) } # 批量获取数据 citation_info <- mapply(get_citation_info, professors$name, professors$school, professors$scholar_id, SIMPLIFY = FALSE) citation_info <- do.call(rbind, citation_info) # 输出结果 print(citation_info)
3. 辅助:自动搜索获取ID
如果手动查找ID麻烦,可先用search_scholar()函数搜索关键词,再提取匹配结果的ID:
# 搜索作者 search_res <- search_scholar("John Smith Harvard University") # 提取第一个匹配结果的ID target_id <- search_res$scholar_id[1]
4. 防反爬提示
Google Scholar有反爬限制,频繁请求可能被拦截,建议在函数中添加延迟:
get_citation_info <- function(name, school, scholar_id) { Sys.sleep(2) # 每次请求间隔2秒,避免触发反爬限制 profile <- get_profile(scholar_id) # 后续代码不变... }
内容的提问来源于stack exchange,提问作者Fox_Summer
相关产品推荐
相关产品推荐

