如何在R中用它的名字解析HTML <script>中的Javascript变量?

时间:2017-04-23 14:44:26

标签: r xpath

我想检索HTML网页中内联的Javascript变量的内容。

&#xA;&#xA;

我可以通过它的位置检索&lt; script&gt; 节点,但我想知道是否有更灵活的方法可以看在变量名本身 - 节点位置可能会改变。

&#xA;&#xA;

例如,页面URL是 https://poloniex.com/marginTrading ,我正在寻找变量 markets_currencies 的内容,其中包含可以归档的所有货币。此信息无法通过API获取。

&#xA;&#xA;
 &lt; script type =“text / javascript”&gt;&#xA; var loggedIn = false;&# xA; var dark = false;&#xA; var mobile = false;&#xA; var mobileDetected = false;&#xA; var usid = false;&#xA; var markets = {};&#xA; var markets_currencies = {“bySymbol”:{“1CR”:{“id”:1,“symbol”:“1CR”,“name”:“1CRedit”,“canLend”:0},“ABY”:{“id”: 2,“symbol”:“ABY”,“name”:“ArtByte”,“canLend”:0} ...}};&#xA; if(window.top!== window.self)window.top。 location.replace(window.self.location.href);&#xA;&lt; / script&gt;&#xA;  
&#xA;&#xA;

到现在为止我检索了这样的网页:

&#xA;&#xA;
  html&lt;  -  getURL(“https://poloniex.com/marginTrading”,followlocation = TRUE)&#xA; doc = htmlParse(html,asText = TRUE)&#xA; plaintext&lt;  -  xpathSApply(doc,“// script”,xmlValue)&#xA; cat(paste(plaintext [[5]],collapse =“\ n”))&#xA;  
&#xA;&#xA;

但是直到更进一步我意识到存储变量的&lt; script&gt; 节点位置可能会发生变化,这不是一个灵活的解决方案。实现这一目标的最佳方法是什么?

&#xA;&#xA;

谢谢,

&#xA;

0 个答案:

没有答案