r语言 - 是否可以抓取特定主题的所有谷歌学术搜索结果,是否合法?



我有一些Rexperience,但没有网站编码,并且认为我无法选择正确的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)

最终,如果我能将所有学者结果抓取并划分为一个带有"标题"、"作者"、"年份"、"期刊"等标题的 csv 文件,那就太好了。任何帮助将不胜感激!谢谢

关于你的代码,你几乎拥有它 - 你没有选择正确的元素。我相信您通过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)

相关内容

最新更新