html - 解析用 rvest 抓取的 Google Scholar 搜索结果
问题描述
我正在尝试使用rvest
将 Google Scholar 搜索结果的一页抓取到作者、论文标题、年份和期刊标题的数据框中。
下面的简化的、可重现的示例是在 Google Scholar 中搜索示例术语“顶级捕食者保护”的代码。
注意:为了遵守服务条款,我只想处理从手动搜索中获得的搜索结果的第一页。我不是在问自动化来抓取额外的页面。
以下代码已经可以提取:
- 作者
- 论文题目
- 年
但它没有:
- 期刊名称
我想提取期刊标题并将其添加到输出中。
library(rvest)
library(xml2)
library(selectr)
library(stringr)
library(jsonlite)
url_name <- 'https://scholar.google.com/scholar?hl=en&as_sdt=0%2C38&q=apex+predator+conservation&btnG=&oq=apex+predator+c'
wp <- xml2::read_html(url_name)
# Extract raw data
titles <- rvest::html_text(rvest::html_nodes(wp, '.gs_rt'))
authors_years <- rvest::html_text(rvest::html_nodes(wp, '.gs_a'))
# Process data
authors <- gsub('^(.*?)\\W+-\\W+.*', '\\1', authors_years, perl = TRUE)
years <- gsub('^.*(\\d{4}).*', '\\1', authors_years, perl = TRUE)
# Make data frame
df <- data.frame(titles = titles, authors = authors, years = years, stringsAsFactors = FALSE)
df
来源:https ://stackoverflow.com/a/58192323/8742237
所以该代码的输出如下所示:
#> titles
#> 1 [HTML][HTML] Saving large carnivores, but losing the apex predator?
#> 2 Site fidelity and sex-specific migration in a mobile apex predator: implications for conservation and ecosystem dynamics
#> 3 Effects of tourism-related provisioning on the trophic signatures and movement patterns of an apex predator, the Caribbean reef shark
#> authors years
#> 1 A Ordiz, R Bischof, JE Swenson 2013
#> 2 A Barnett, KG Abrantes, JD Stevens, JM Semmens 2011
两个问题:
- 如何添加从原始数据中提取期刊标题的列?
- 是否有参考资料可供我阅读并了解有关如何为自己提取其他字段的更多信息,所以我不必在这里问?
解决方案
添加它们的一种方法是:
library(rvest)
library(xml2)
library(selectr)
library(stringr)
library(jsonlite)
url_name <- 'https://scholar.google.com/scholar?hl=en&as_sdt=0%2C38&q=apex+predator+conservation&btnG=&oq=apex+predator+c'
wp <- xml2::read_html(url_name)
# Extract raw data
titles <- rvest::html_text(rvest::html_nodes(wp, '.gs_rt'))
authors_years <- rvest::html_text(rvest::html_nodes(wp, '.gs_a'))
# Process data
authors <- gsub('^(.*?)\\W+-\\W+.*', '\\1', authors_years, perl = TRUE)
years <- gsub('^.*(\\d{4}).*', '\\1', authors_years, perl = TRUE)
leftovers <- authors_years %>%
str_remove_all(authors) %>%
str_remove_all(years)
journals <- str_split(leftovers, "-") %>%
map_chr(2) %>%
str_extract_all("[:alpha:]*") %>%
map(function(x) x[x != ""]) %>%
map(~paste(., collapse = " ")) %>%
unlist()
# Make data frame
df <- data.frame(titles = titles, authors = authors, years = years, journals = journals, stringsAsFactors = FALSE)
对于您的第二个问题:css 选择器小工具 chrome 扩展非常适合获取所需元素的 css 选择器。但是在您的情况下,所有元素都共享相同的 css 类,因此解开它们的唯一方法是使用正则表达式。所以我想了解一下css选择器和正则表达式:)
推荐阅读
- docker - .net core 3.1 中的数据保护在 Docker 中采用默认设置
- stripe-payments - 条纹 - 找出客户使用的付款方式
- python - Python在线程上显示相机预览
- kubernetes - Openshift 作业存在,但未安排且无法删除
- node.js - GraphQL:几个字段解析器
- google-analytics - 使用相同的 API 检索 GA3 和 GA4 数据?
- java - Android Studio 中的 Proguard 有问题
- css - 关于视口的背景固定,但也滚动
- python - Python - 请求在输出中添加额外字符的前缀
- heroku - nuxt/pwa 部署到 heroku 时无法“离线”工作