R和RVest:抓取html文件

时间:2018-09-10 16:36:25

标签: html r rvest

我尝试从HTML文件在R中创建一个数据框。 HTML文件包含网站的几篇文章,并包含标题和以下各段。我想在向量中分别写上标题和全文。

我使用rvest以及html_nodes命令和CSS格式命令,到目前为止效果很好。但是我无法创建一个数据框,因为标题和段落的数量不一样:一篇文章在逻辑上由几个段落组成,并且每个文章的编号都不同。

如何编写向R解释的代码,我想将文章的所有段落汇总为一个向量?

这是我到目前为止的代码:

site <- read_html("Local Path")

heading <- html_text(html_nodes(x=site, ".counted"))
heading <- gsub('\"', "", heading, fixed = TRUE)
heading  

fulltext <- html_text(html_nodes(x=site, ".dearticleParagraph"))
fulltext <- gsub("\r\n", "", fulltext, fixed = TRUE)
head(fulltext)

dataframe <-data.frame(Heading = heading, Full Text = fulltext, stringsAsFactors = FALSE))

您可以在此处找到HTML文件的示例:https://seafile.zfn.uni-bremen.de/f/2648cd4c7a7a429a9c7d/?dl=1

非常感谢。

最好的问候, 回声

0 个答案:

没有答案