如何使用jsoup从HTML解析表

  5,390.85 5,428.15 5,376.15 5,413.85 

这是HTML源代码,我必须从中提取值5390.85,5428.15,5376.15,5413.85。 我想用jsoup来做这件事。 但我对jsoup相对较新(今天我开始使用它)。 那我该怎么做呢?

 URL url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm"); Document doc = Jsoup.parse(url,3*1000); String text = doc.body().text(); 

我已经使用jsoup提取了网站的内容。 但如何提取我需要的值? 提前致谢

尝试这样的事情: –

 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 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()); 

这是打印输出: –

 Value 1: 5,390.85 Value 2: 5,428.15 Value 3: 5,376.15 Value 4: 5,413.85 

这是使用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() } } } 

您可以将其保存到arrays以进行进一步处理。 只是另一种观点。