【发布时间】:2019-07-02 17:35:30
【问题描述】:
到目前为止,我使用RSelenium 来提取主页的文本,但我想切换到像rvest 这样的快速解决方案。
library(rvest)
url = 'https://www.r-bloggers.com'
rvestResults <- read_html(url) %>%
html_node('body') %>%
html_text()
library(RSelenium)
remDr$navigate(url)
rSelResults <- remDr$findElement(
using = "xpath",
value = "//body"
)$getElementText()
比较下面的结果表明 rvest 包含一些 JavaScript 代码,而 RSelenium 更“干净”。
我知道 rvest 和 rselenium 之间的区别,rselenium 使用无头浏览器,而 rvest 只读取“普通主页”。
我的问题是:有没有一种方法可以让我通过 rvest 获得下面的 Rselenium 输出,或者与第三种方式的 rvest 一样快(或更快)?
研究结果:
> substring(rvestResults, 1, 500)
[1] "\n\n\n\t\t \t \t\n \n R news and tutorials contributed by (750) R bloggers \n Home\nAbout\nRSS\nadd your blog!\nLearn R\nR jobs\nSubmit a new job (it’s free)\n\tBrowse latest jobs (also free)\n\nContact us\n\n\n\n\n\n\n\n \n\t\tWelcome!
\t\t\t\r\nfunction init() {\r\nvar vidDefer = document.getElementsByTagName('iframe');\r\nfor (var i=0; i<vidDefer.length; i++) {\r\nif(vidDefer[i].getAttribute('data-src'))
{\r\nvidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));\r\n} } }\r\nwindow.onload = i"
RSelenium 结果:
> substring(rSelResults, 1, 500)
[1] "R news and tutorials contributed by (750) R bloggers\nHome\nAbout\nRSS\nadd your blog!\nLearn R\nR jobs\n�\n�\n�\nContact us\nWELCOME!\nHere you will find daily news and tutorials about R,
contributed by over 750 bloggers.\nThere are many ways to follow us -\nBy e-mail:\nOn Facebook:\nIf you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English
R bloggers should add themselves- here)\nJOBS FOR R-USERS\nData/GIS Analyst for Ecoscape Environmental Consultants @ Kelowna, "
【问题讨论】: