【问题标题】:rvest vs RSelenium results for text extractingrvest vs RSelenium 文本提取结果
【发布时间】: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, "

【问题讨论】:

    标签: r rvest rselenium


    【解决方案1】:

    也许webdriver,这是一个 PhantomJS 实现,会做得更好(目前无法针对 RSelenium 进行测试):

    library("webdriver")
    library("rvest")
    
    pjs <- run_phantomjs()
    ses <- Session$new(port = pjs$port)
    url <- 'https://www.r-bloggers.com'
    ses$go(url)
    
    res <- ses$getSource() %>% 
      read_html() %>%
      html_node('body') %>%
      html_text()
    
    substring(res, 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\n\n\n\n\nHere you will find daily news and tutorials about R, contributed by over 750 bloggers. \n\nThere are many ways to follow us - \nBy e-mail:\n\n\n<img src=\"https://feeds.feedburner.com/~fc/RBloggers?bg=99CCFF&amp;fg=444444&amp;anim=0\" height=\"26\" width=\"88\" sty"
    

    【讨论】:

    • 感谢您的回答。这确实是一种改进。但是,res 中仍然有 JavaScript 代码,所以如果可以,我会赞成但不接受,...
    • 没关系。我很高兴在这里看到比我自己更好的解决方案。但是出于兴趣,你在哪里看到 JS 代码在 res 中?我可以看到一些 HTML 块,但没有 JS。
    【解决方案2】:

    您可以尝试使用正则表达式来清理您的数据,

    url <- "https://www.r-bloggers.com"
    
    res <- url %>% 
      read_html() %>% 
      html_nodes('body') %>%
      html_text()
    
    library(stringr)
    
    # clean up text data
    res %>%
      str_replace_all(pattern = "\n", replacement = " ") %>%
      str_replace_all(pattern = "[\\^]", replacement = " ") %>%
      str_replace_all(pattern = "\"", replacement = " ") %>%
      str_replace_all(pattern = "\\s+", replacement = " ") %>%
      str_trim(side = "both")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      相关资源
      最近更新 更多