【发布时间】:2021-02-19 01:51:15
【问题描述】:
我正在使用rstudio 0.99.879 版并使用包rvest 0.3.2 版执行以下任务(我是网络抓取技术的初学者):
对于一个研究项目,我想抓取期刊网站以提取特定文章的作者、机构隶属关系等信息。
我通过以下方式执行此操作:
#Specifying the url for desired website to be scraped (article)
webpage001 <- read_html("https://link.springer.com/article/10.1007/s12286-017-0325-1")
#Using CSS selectors to scrape the rankings section (with Abstract)
abstract_html001 <- html_node(webpage001,".Para")
authors_html001 <- html_nodes(webpage001,".authors__name")
affiliation_html001 <- html_nodes(webpage001, ".affiliation__item")
#Converting the title data to text
abstract001 <- html_text(abstract_html001)
authors001 <- html_text(authors_html001)
affiliation001 <- html_text(affiliation_html001)
# creating a data frame
text01 <- data.frame(Abstract = abstract001, Author = authors001,
Institution = affiliation001)
text01
这很好用,我得到了一个 obs。和三个变量。但也有一些学术文本,例如书评,不包含摘要。如果我尝试相同的评论,
webpage002 <- read_html("https://link.springer.com/article/10.1007/s12286-017-0324-2")
authors_html002 <- html_nodes(webpage002,".authors__name")
affiliation_html002 <- html_nodes(webpage002, ".affiliation__item")
authors002 <- html_text(authors_html002)
affiliation002 <- html_text(affiliation_html002)
# creating a data frame
text02 <- data.frame(Author = authors001, Institution = affiliation001)
text02
我得到一个 obs。和两个变量。最后,我想合并两个数据框,但由于列数不相等,rbind 函数不起作用。
对于一些文本,我可以指定以下
abstract002 <- NA
text02 <- data.frame(Abstract = abstract002, Author = authors002,
Institution = affiliation002)
text02
total <- rbind(text01, text02)
但是,对于大量的文本,这太多了,我想知道是否有办法以不同的方式甚至是半自动化的(例如,每次文本都没有摘要,NA 被分配到正确的列中)。
有没有人知道如何做到这一点或解决它?
提前致谢!
【问题讨论】:
标签: rstudio rvest rbind r web-scraping rvest