rselenium |获取网页的文字

时间:2016-02-07 22:14:58

标签: r phantomjs plaintext rselenium

有没有办法从RSelenium中的remoteDriver获取纯文本? 有些人认为:remDr$getPlainText()等同于remDr$getPageSource()

Workarount:

我设法将phantomJS的plainText保存到文件中,如下所示:

  require(RSelenium)
  pJS <- phantom()
  Sys.sleep(5) # give the binary a moment
  remDr = remoteDriver(browserName = 'phantomjs')
  remDr$open()
  remDr$phantomExecute('var page = this;
                         var fs = require(\"fs\");
                       page.onLoadFinished = function(status) {
                       var txtFile = fs.open(\"url.txt\", \"w\");
                       txtFile.write(page.plainText);
                       txtFile.close();
                       };')

  remDr$navigate(some_url)

但是后来我必须在后面读取文件......

我的解决方法类似于https://cran.r-project.org/web/packages/RSelenium/vignettes/RSelenium-headless.html#id3b

1 个答案:

答案 0 :(得分:4)

我不确定它是否能解决问题。

library(RSelenium)
checkForServer()
startServer()
re<-remoteDriver()
re$open()
re$navigate("link")
txt<-re$findElement(using='css selector',"body")$getElementText()