Using Jsoup to extract data

狂风中的少年 提交于 2019-12-11 18:58:26

问题


I am using jsoup to extract data from a table in a website.http://www.moneycontrol.com/stocks/marketstats/gainerloser.php?optex=BSE&opttopic=topgainers&index=-1 using Jsoup. I have referred to Using JSoup To Extract HTML Table Contents and other similar questions but it does not print the data. Could someone please provide me with the code required to achieve this?

public class TestClass
 {


public static void main(String args[]) throws IOException
{
Document doc = Jsoup.connect("http://www.moneycontrol.com/stocks/marketstats/gainerloser.php?optex=BSE&opttopic=topgainers&index=-1").get();

    for (Element table : doc.select("table.tablehead")) {
        for (Element row : table.select("tr")) {
            Elements tds = row.select("td");
            if (tds.size() > 6) {
                System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
            }
        }
    }

回答1:


If you want to get the content of table(not head), you need change the selector of table:

for (Element table : doc.select("table.tbldata14"))

instead of

 for (Element table : doc.select("table.tablehead"))



回答2:


One important thing is to check what are you getting in Doc when you parse the HTML because there might be few problems with it like: 1. The Site might be using iframes to display content 2. Display content via Javascript 3. few sites have scripts which does not allow jsoup parsing, hence the doc element will contain random data



来源:https://stackoverflow.com/questions/9358590/using-jsoup-to-extract-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!