htmlunit.WebClient 抓不到vue生成的页面

yak 发布于 09/13 21:35
阅读 120
收藏 0

测试页面

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id="msg" ></div>
<div id="app">
  <p>{{ message }}</p>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  }
})
document.getElementById('msg').innerHTML="xxx";

</script>
</body>
</html>

抓取代码

import java.util.*;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
 
class WcPage {
	public static void main(String args[]) throws Exception
	{

 
 
String  url="http://localhost/vue/vue2.html";


WebClient wc = new WebClient();
   wc.getOptions().setJavaScriptEnabled(true); //启用JS解释器,默认为true  
   

HtmlPage page = wc.getPage(url);
Thread.sleep(1000);
String pageAsXml = page.asXml();

//System.out.print(pageAsXml);
List<HtmlDivision> eles =  page.getByXPath("//div");

  for(HtmlElement ele :eles){

     System.out.print(ele.asXml());
  }
   

	 
	}
}

 

输出结果

只有xxx 没有ooo

 

加载中
返回顶部
顶部