我有一些经验,但没有网站编码经验,并且认为我无法选择正确的 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_rt
and gs_a
.
With 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)