如何用Java从网站获取数据?
我想得到“”中“收益率”的值 如何使用java实现这一点 我尝试过“Jsoup”和我的代码,如下所示:如何用Java从网站获取数据?,java,Java,我想得到“”中“收益率”的值 如何使用java实现这一点 我尝试过“Jsoup”和我的代码,如下所示: public static void main(String[] args) throws IOException { String url = "http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319"; Document document = Jsoup.connect(url).get(); Eleme
public static void main(String[] args) throws IOException {
String url = "http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319";
Document document = Jsoup.connect(url).get();
Elements answerers = document.select(".c3 .floatR ");
for (Element answerer : answerers) {
System.out.println("Answerer: " + answerer.data());
}
// TODO code application logic here
}
但它返回空的。如何实现这一点?最简单的解决方案是创建一个URL实例,指向您希望使用流获取内容的网页/链接- 比如说-
public static void main(String[] args) throws IOException
{
URL url = new URL("http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319");
// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
// Once you have the Input Stream, it's just plain old Java IO stuff.
// For this case, since you are interested in getting plain-text web page
// I'll use a reader and output the text content to System.out.
// For binary content, it's better to directly read the bytes from stream and write
// to the target file.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
// read each line and write to System.out
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
我认为Jsoup在这方面至关重要。我不会怀疑一个有效的HTML文档(或任何东西) 您的代码很好。我自己测试过。问题是您正在使用的URL。如果在浏览器中打开url,则值字段(例如收益率)为空。使用浏览器开发工具(网络选项卡),您应获得如下URL:
使用此URL可获得所需的结果。请更具体一点:返回的文档是否为空(null);或者“应答者”,或者所有单个应答对象?尝试RTQ(capitalic)这里是结果:运行:应答者:应答者:应答者:应答者:应答者:应答者:构建成功(总时间:0秒),我如何才能获得“屈服”值?我也已经厌倦了你的代码。我有HTML。屈服位置的值为空……结果是:运行:应答器:应答器:应答器:应答器:应答器:应答器:应答器:应答器:构建成功(总时间:0秒)抱歉,我找到了原因,这应该是System.out.println(“应答器:+Answer.text());它是固定的。