简体   繁体   English

Java使用jsoup解析html表中的数据

[英]Java parse data from html table with jsoup

I want to get the data from the table from the link. 我想从链接的表中获取数据。

link: 链接:

https://www.nasdaq.com/symbol/aapl/financials?query=balance-sheet https://www.nasdaq.com/symbol/aapl/financials?query=balance-sheet

I´ve tried my code but it doens´t work 我已经尝试过我的代码,但是没有用

public static void main(String[] args) {
    try {
        Document doc = Jsoup.connect("https://www.nasdaq.com/symbol/aapl/financials?query=balance-sheet").get();
        Elements trs = doc.select("td_genTable");



        for (Element tr : trs) {
            Elements tds = tr.getElementsByTag("td");
            Element td = tds.first();
            System.out.println(td.text());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Can anybody help me? 有谁能够帮助我? To get it to work 为了使其正常工作

I´m not getting an output of the table. 我没有得到该表的输出。 Nothing happens. 什么都没发生。

After test your code I've got and Read time out problem. 测试您的代码后,我得到了Read time out问题。 Looking on Google I found this post where suggest to add an user agent to fix it and it worked for me . 在Google上查找时,我发现了这篇文章 ,建议添加一个用户代理对其进行修复,它对我有用 So, you can try this 所以,你可以试试看

public static void main(String[] args) {
    try {
        // add user agent
        Document doc = Jsoup.connect("https://www.nasdaq.com/symbol/aapl/financials?query=balance-sheet")
                .userAgent("Mozilla/5.0").get();
        Elements trs = doc.select("tr");
        for (Element tr : trs) {
            Elements tds = tr.select(".td_genTable");
            // avoid tr headers that produces NullPointerException
            if(tds.size() == 0) continue;
            // look for siblings (see the html structure of the web)
            Element td = tds.first().siblingElements().first();
            System.out.println(td.text());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I have added User agent option and fix some query errors . 我添加了用户代理选项,并修复了一些查询错误 This will be useful to start your work ;) 这将对您开始工作很有用;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM