如何从Java获取网站数据?

时间:2016-06-07 09:02:12

标签: java

我想在“http://www.aastocks.com/en/ltp/rtquote.aspx?symbol=01319”中获得“收益”的值 我怎么能用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();

    Elements answerers = document.select(".c3 .floatR ");
    for (Element answerer : answerers) {
        System.out.println("Answerer: " + answerer.data());
    }
    // TODO code application logic here
}

但它返回空白。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

你的代码很好。我亲自测试过。问题是您正在使用的URL。如果我在浏览器中打开网址,则值字段(例如,Yield)为空。使用浏览器开发工具(“网络”选项卡),您应该获得如下所示的URL:

  

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


使用此URL可以获得想要的结果。

答案 1 :(得分:-1)

最简单的解决方案是创建一个指向您希望使用流获取内容的网页/链接的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);
    }
}

答案 2 :(得分:-1)

我认为Jsoup在这方面至关重要。我不会怀疑有效的HTML文档(或其他)。