【发布时间】:2014-01-05 00:24:22
【问题描述】:
我正在尝试从 Java 调用 phantomjs 脚本,但不幸的是进程挂在下面的片段中:
var ua = page.evaluate(function () {
return document.getElementsByTagName('html')[0].outerHTML;
});
你有什么想法吗?剩下的就在这里:
加载速度.js:
var page = require('webpage').create(),
system = require('system'),
t, address;
t = Date.now();
address = "http://query.nytimes.com/search/sitesearch/#/africa+floods";
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
var ua = page.evaluate(function () {
return document.getElementsByTagName('html')[0].outerHTML;
});
console.log(ua);
}
phantom.exit();
});
Java:
public static void main(String[] args) throws IOException, InterruptedException, ScriptException {
String command="cmd /c phantomjs loadspeed.js";
Process process = Runtime.getRuntime().exec(command);
int exitStatus = process.waitFor();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String currentLine=null;
StringBuilder stringBuilder = new StringBuilder(exitStatus==0?"SUCCESS:":"ERROR:");
currentLine= bufferedReader.readLine();
while(currentLine !=null)
{
stringBuilder.append(currentLine);
currentLine = bufferedReader.readLine();
}
System.out.println(stringBuilder.toString());
【问题讨论】:
标签: java javascript web-scraping phantomjs