how to parse a table from HTML using jsoup

前端 未结 2 1781
旧时难觅i
旧时难觅i 2020-12-08 08:27

 
5,390.85


        
相关标签:
2条回答
  • 2020-12-08 08:32

    Here's an example using Groovy lang:

    def url = "http://www.espn.co.uk/scrum/rugby/match/scores/recent.html"
    def doc = Jsoup.connec(url).get()
    
    //Strip the table from the page
    def table = doc.select("table").first()
    // Strip the rows from the table
    def tbRows = table.select("tr")
    
    // For each column in a row, print its contents if not empty
    tbRows.each { row ->
        def tbCol = row.select("td")
        tbCol.each { column ->
            if(!column.text().empty) {
                println column.text()
            }
        }
    }
    

    You could save this to an array for further processing. Just another perspective.

    0 讨论(0)
  • 2020-12-08 08:42

    Try something like this:-

    URL url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm");
    Document doc = Jsoup.parse(url, 3000);
    
    Element table = doc.select("table[class=niftyd]").first();
    
    Iterator<Element> ite = table.select("td[width=65]").iterator();
    
    ite.next(); // first one is image, skip it
    
    System.out.println("Value 1: " + ite.next().text());
    System.out.println("Value 2: " + ite.next().text());
    System.out.println("Value 3: " + ite.next().text());
    System.out.println("Value 4: " + ite.next().text());
    

    Here's the printout:-

    Value 1: 5,390.85
    Value 2: 5,428.15
    Value 3: 5,376.15
    Value 4: 5,413.85
    
    0 讨论(0)
提交回复
热议问题