使用 Java 从网站获取 InnerHTMLof JS
Get InnerHTMLof JS from Website using Java
目标:从 Yahoo 财经页面获取 JavaScript 元素的内部文本。请参考
我可以使用下面的代码获取 innerHTML
document.getElementsByClassName('D(ib) Va(t)')[15].childNodes[2].innerHTML
但是,我找不到将此信息传送到 Java
中的 Yahoo Finance 页面的方法
我简要地尝试了以下 API:
- JSoup
- HTMLUnit
- Nashorn
我认为 Nashorn 可以得到我要找的文本,但我还没有做到。
如果有人做过类似的事情或者能指出我正确的方向,那将不胜感激。
如果需要更多详细信息,请告诉我。
HtmlUnit 似乎对该站点有问题,因为响应也不完整。你可以使用 PhantomJS. Just download the binary for your OS and create a script file (see API).
脚本 (yahoo.js
):
var page = require('webpage').create();
var fs = require('fs');
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.settings.resourceTimeout = '5000';
page.open('http://finance.yahoo.com/quote/AAPL/profile?p=AAPL', function(status) {
console.log("Status: " + status);
if(status === "success") {
var path = 'yahoo.html';
fs.write(path, page.content, 'w');
}
phantom.exit();
});
Java代码:
try {
//change path to phantomjs binary and your script file
String phantomJSPath = "bin" + File.separator + "phantomjs";
String scriptFile = "yahoo.js";
Process process = Runtime.getRuntime().exec(phantomJSPath + " " + scriptFile);
process.waitFor();
//Jsoup
Elements elements = Jsoup.parse(new File("yahoo.html"),"UTF-8").select("div.asset-profile-container p strong"); //yahoo.html created by script file in same path
for (Element element : elements) {
if(element.attr("data-reactid").contains("asset-profile.1.1.1.2")){
System.out.println(element.text());
}
}
} catch (Exception e) {
e.printStackTrace();
}
输出:
Consumer Goods
注:
以下 link returns 一个包含公司信息的 JSONObject,但不确定 crumb
参数是否更改或对公司保持不变:
https://query2.finance.yahoo.com/v10/finance/quoteSummary/AAPL?formatted=true&crumb=hm4%2FV0JtzlL&lang=en-US®ion=US&modules=assetProfile%2CsecFilings%2CcalendarEvents&corsDomain=finance.yahoo.com
目标:从 Yahoo 财经页面获取 JavaScript 元素的内部文本。请参考
我可以使用下面的代码获取 innerHTML
document.getElementsByClassName('D(ib) Va(t)')[15].childNodes[2].innerHTML
但是,我找不到将此信息传送到 Java
中的 Yahoo Finance 页面的方法我简要地尝试了以下 API:
- JSoup
- HTMLUnit
- Nashorn
我认为 Nashorn 可以得到我要找的文本,但我还没有做到。
如果有人做过类似的事情或者能指出我正确的方向,那将不胜感激。
如果需要更多详细信息,请告诉我。
HtmlUnit 似乎对该站点有问题,因为响应也不完整。你可以使用 PhantomJS. Just download the binary for your OS and create a script file (see API).
脚本 (yahoo.js
):
var page = require('webpage').create();
var fs = require('fs');
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.settings.resourceTimeout = '5000';
page.open('http://finance.yahoo.com/quote/AAPL/profile?p=AAPL', function(status) {
console.log("Status: " + status);
if(status === "success") {
var path = 'yahoo.html';
fs.write(path, page.content, 'w');
}
phantom.exit();
});
Java代码:
try {
//change path to phantomjs binary and your script file
String phantomJSPath = "bin" + File.separator + "phantomjs";
String scriptFile = "yahoo.js";
Process process = Runtime.getRuntime().exec(phantomJSPath + " " + scriptFile);
process.waitFor();
//Jsoup
Elements elements = Jsoup.parse(new File("yahoo.html"),"UTF-8").select("div.asset-profile-container p strong"); //yahoo.html created by script file in same path
for (Element element : elements) {
if(element.attr("data-reactid").contains("asset-profile.1.1.1.2")){
System.out.println(element.text());
}
}
} catch (Exception e) {
e.printStackTrace();
}
输出:
Consumer Goods
注:
以下 link returns 一个包含公司信息的 JSONObject,但不确定 crumb
参数是否更改或对公司保持不变:
https://query2.finance.yahoo.com/v10/finance/quoteSummary/AAPL?formatted=true&crumb=hm4%2FV0JtzlL&lang=en-US®ion=US&modules=assetProfile%2CsecFilings%2CcalendarEvents&corsDomain=finance.yahoo.com