問題:
有些網頁數據是由js動態生成的,一般我們抓包可以看出真正的數據實體是由哪一個異步請求獲取到的,但是獲取數據的請求鏈接也可能由其他js產生,這個時候我們希望直接拿到js加載后的最終網頁數據。
解決方法:
phantomjs
1.下載phantomjs,[官網]:http://phantomjs.org/
2.我們是windows平台,解壓,會在bin目錄下看到exe可執行文件,有它就夠啦。
3.寫一個parser.js:
system = require('system')
address = system.args[1];
var page = require('webpage').create();
var url = address;
page.settings.resourceTimeout = 1000*10; // 10 seconds
page.onResourceTimeout = function(e) {
console.log(page.content);
phantom.exit(1);
};
page.open(url, function (status) {
//Page is loaded!
if (status !== 'success') {
console.log('Unable to post!');
} else {
console.log(page.content);
}
phantom.exit();
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4.java調用
Runtime rt = Runtime.getRuntime();
Process process = null;
try {
process = rt.exec("C:/phantomjs.exe C:/parser.js " +url);
InputStream in = process.getInputStream();
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(reader);
StringBuffer sbf = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sbf.append(tmp);
}
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
————————————————
版權聲明:本文為CSDN博主「ylzhusky」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/zeroctu/article/details/53818185