如何使用jsoup从HTML解析表 [英] how to parse a table from HTML using jsoup
本文介绍了如何使用jsoup从HTML解析表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<td width="10"></td>
<td width="65"><img src="/images/sparks/NIFTY.png" /></td>
<td width="65">5,390.85</td>
<td width="65">5,428.15</td>
<td width="65">5,376.15</td>
<td width="65">5,413.85</td>
这是我必须提取值5390.85,5428.15,5376.15,5413.85的HTML源代码。
我想用jsoup来做这件事。但我对jsoup相对较新(今天我开始使用它)。那我该怎么做?
This is the HTML source from which i have to extract the values 5390.85,5428.15 , 5376.15 , 5413.85. I wanted to do this using jsoup. But i am relatively new to jsoup( today i started using it). So how should i do this?
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提取了网站的内容。
但是如何提取我需要的值?
提前致谢
I have already extracted the content of the website using jsoup. but how to extract the values i require? Thanks in advance
推荐答案
尝试这样的事情: -
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
这篇关于如何使用jsoup从HTML解析表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文