2016-11-22 59 views
1

我:如何使用R从JavaScript饼图中抓取Web数据?

library(XML) 

my_URL <- "http://www.velocitysharesetns.com/viix" 

tables <- readHTMLTable(my_URL) 

PieChart

上述输出只是位于页面顶部的表。它看起来像饼图被忽略,并且javascript解释它。有没有简单的解决方案来提取图表中的两个百分比数字?

看了看RSelenium但是我收到一些错误,我一直没能找到任何解决方案。

> RSelenium::startServer() 
Error in if (file.exists(file) == FALSE) if (!missing(asText) && asText == : 
    argument is of length zero 
In addition: Warning messages: 
1: startServer is deprecated. 
Users in future can find the function in file.path(find.package("RSelenium"), "example/serverUtils"). 
The sourcing/starting of a Selenium Server is a users responsiblity. 
Options include manually starting a server see vignette("RSelenium-basics", package = "RSelenium") 
and running a docker container see vignette("RSelenium-docker", package = "RSelenium") 
2: running command '"java" -jar "\\med-fs01/Home/Alex.Badoi/R/win-library/3.3/RSelenium/bin/selenium-server-standalone.jar" -log "\\med-fs01/Home/Alex.Badoi/R/win-library/3.3/RSelenium/bin/sellog.txt"' had status 127 
3: running command '"wmic" path win32_process get Caption,Processid,Commandline /format:htable' had status 44210 
> 

根据菲利普的回答我想出了一个流动的解决方案:

library(XML) 



# extarct HTML 

doc.html = htmlTreeParse('http://www.velocitysharesetns.com/viix', 
         useInternal = TRUE) 


# convert to text 

htmltxt <- paste(capture.output(doc.html, file=NULL), collapse="\n") 

# get location of string 

pos = regexpr('CBOE SHORT-TERM VIX FUTURE', htmltxt) 

# extarct from "pos" to nchar to end of string 

keep = substr(htmltxt, pos, pos+98) 

输出:

> keep 
[1] "CBOE SHORT-TERM VIX FUTURE DEC 2016', 81.64],\n\n ['CBOE SHORT-TERM VIX FUTURE JAN 2017', 18.36],\n" 
+0

在运行'startServer()'之前,你运行'checkForServer(update = TRUE)'吗? 此外,看看你的浏览器(例如Firefox的F12)的检查员,看看你想要获取的数据是否可以在那里被识别。 – PhillipD

+0

嗨Phillip,好像checkForServer命令也被弃用。对于使用chrome的第二个问题,右键单击,检查元素。我不知道任何Java,但2代码不显示在代码中。 –

回答

3

使用RSelenium

这种解决方案我使用Rselenium为(使用Windows 7并检查源代码后e网页)。请注意,我用chromedriver.exe

library(RSelenium) 
checkForServer(update = TRUE) 

#### I use Chromedriver 
startServer(args = c("-Dwebdriver.chrome.driver=C:/Stuff/Scripts/chromedriver.exe")) 

remDr <- remoteDriver(remoteServerAddr = "localhost", browserName="chrome", port=4444) 

### Open Chrome 
remDr$open() 

remDr$navigate("http://www.velocitysharesetns.com/viix") 

b <- remDr$findElements(using="class name", value="jqplot-pie-series") 

sapply(b, function(x){x$getElementAttribute("outerHTML")}) 

最后的命令返回

[[1]] 
[1] "<div class=\"jqplot-pie-series jqplot-data-label\" style=\"position: absolute; left: 100px; top: 106px;\"><div style=\"color:white;font-weight:bold;\">82%</div></div>" 

[[2]] 
[1] "<div class=\"jqplot-pie-series jqplot-data-label\" style=\"position: absolute; left: 159px; top: 67px;\"><div style=\"color:white;font-weight:bold;\">18%</div></div>" 

你可以看到百分比数字出现在那里,可以很容易提取。

只需使用普通的HTML

的数据。另外还可以通过,因为数据已经包含在读取了HTML源中获取。在源代码中你可以找到:

<script type="text/javascript" language="javascript"> 
$(document).ready(function(){ 
var data = [ 


['CBOE SHORT-TERM VIX FUTURE DEC 2016', 81.64], 

['CBOE SHORT-TERM VIX FUTURE JAN 2017', 18.36], 

]; 

这就是你要找的。数字在图中显示之前被舍入。

+0

我用HTML位来提出我自己的解决方案。看到有问题的编辑。不幸的是Selenium没有为我工作,但我相信这是一个基于你的解决方案的工作答案。谢谢您的帮助。 –