是否可以抓取特定主题的所有谷歌学术结果并且合法吗?

Aak*_*ity 5 r web-scraping google-scholar rvest

我有一些经验,但没有网站编码经验,并且认为我无法选择正确的 CSS 节点进行解析(我相信)。

library(rvest)
library(xml2)
library(selectr)
library(stringr)
library(jsonlite)

url <-'https://scholar.google.com/scholar?hl=en&as_sdt=0%2C38&q=apex+predator+conservation&btnG=&oq=apex+predator+c'
webpage <- read_html(url)

title_html <- html_nodes(webpage, 'a#rh06x-YUUvEJ')
title <- html_text(title_html)
head(title)
Run Code Online (Sandbox Code Playgroud)

最终,如果我可以将所有学者成果抓取并分成一个 csv 文件,其中包含“标题”、“作者”、“年份”、“期刊”等标题,那就太好了。任何帮助将非常感激!谢谢

nik*_*iko 5

关于您的代码,您几乎已经完成了 - 您没有选择正确的元素。我相信您在选择 by 时选择了id我发现html_nodes效果最好的地方class。您正在寻找的课程是gs_rtgs_a

然后regex,您可以通过提取作者和年份将数据处理为所需的格式。

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)
Run Code Online (Sandbox Code Playgroud)