首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Java网站获取数据?

如何从Java网站获取数据?
EN

Stack Overflow用户
提问于 2016-06-07 09:02:11
回答 3查看 68关注 0票数 0

我想在"http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319“中获得”收益率“的值

我已经尝试过"Jsoup“,我的代码如下:

代码语言:javascript
复制
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
}

但它是空的。我该怎么做?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-07 09:20:08

你的密码没问题。我亲自测试过。问题在于你使用的URL。如果我在浏览器中打开url,则值字段(例如收益率)为空。使用浏览器开发工具(网络选项卡),您应该获得如下URL:

http://www.aastocks.com/en/ltp/RTQuoteContent.aspx?symbol=01319&process=y

使用这个URL会给出想要的结果。

票数 1
EN

Stack Overflow用户

发布于 2016-06-07 09:10:49

最简单的解决方案是创建一个指向您想要的网页/链接的URL实例,使用流获取内容-

例如-

代码语言:javascript
复制
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);
    }
}
票数 -1
EN

Stack Overflow用户

发布于 2016-06-07 09:13:03

我认为珍汤在这方面是至关重要的。我不会怀疑一个有效的HTML文档(或其他什么)。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37674919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档