如何获取表的前 3 个 td 值?
How to fetch the tables first 3 td values?
我正在对下面的数据进行抓取 table
<table width="100%" class="indexes">
<thead>
<tr>
<th></th>
<th>Last</th>
<th>Chg</th>
<th>Chg %</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<tr class="alt">
<td class="indexes-arrow">
<div class="arrow_positive"></div>
</td>
<td class="indexprice fleft positive">7,851.50</td>
<td class="indexchange fleft positive">+0.50</td>
<td class="indexpercent fleft positive">+0.01%</td>
<td class="indexhigh fleft">7,888.50</td>
<td class="indexlow fleft">7,818.00</td>
</tr>
</tbody>
</table>
我需要获取前 3 个 td 值,即 7,851.50 、+0.50 和 +0.01%
我正在尝试这种方式
try
{
Document doc = null;
doc = Jsoup.connect("http://sgxnifty.org/")
.timeout(10 * 1000).get();
String tickerEl = doc.select("indexes tbody tr indexprice fleft positive").text();
System.out.println(tickerEl);
}
但是它变空了,能否请您告诉我如何获取前 3 个 td 值??
你能告诉我如何解决这个问题吗??
尝试 select 他们 类
e1 = doc.select("td.indexprice").text();
e2 = doc.select("td.indexchange").text();
e3 = doc.select("td.indexpercent").text();
解决方案
这里更简单,因为您的每个标签都有以下 class positive
for (Element e : doc.getElementsByClass("positive")){
System.out.println(e.text());
}
您也可以select使用索引
e1 = doc.select("tr td:nth-child(2)").text();
e2 = doc.select("tr td:nth-child(3)").text();
e3 = doc.select("tr td:nth-child(4)").text();
我正在对下面的数据进行抓取 table
<table width="100%" class="indexes">
<thead>
<tr>
<th></th>
<th>Last</th>
<th>Chg</th>
<th>Chg %</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<tr class="alt">
<td class="indexes-arrow">
<div class="arrow_positive"></div>
</td>
<td class="indexprice fleft positive">7,851.50</td>
<td class="indexchange fleft positive">+0.50</td>
<td class="indexpercent fleft positive">+0.01%</td>
<td class="indexhigh fleft">7,888.50</td>
<td class="indexlow fleft">7,818.00</td>
</tr>
</tbody>
</table>
我需要获取前 3 个 td 值,即 7,851.50 、+0.50 和 +0.01%
我正在尝试这种方式
try
{
Document doc = null;
doc = Jsoup.connect("http://sgxnifty.org/")
.timeout(10 * 1000).get();
String tickerEl = doc.select("indexes tbody tr indexprice fleft positive").text();
System.out.println(tickerEl);
}
但是它变空了,能否请您告诉我如何获取前 3 个 td 值??
你能告诉我如何解决这个问题吗??
尝试 select 他们 类
e1 = doc.select("td.indexprice").text();
e2 = doc.select("td.indexchange").text();
e3 = doc.select("td.indexpercent").text();
解决方案
这里更简单,因为您的每个标签都有以下 class positive
for (Element e : doc.getElementsByClass("positive")){
System.out.println(e.text());
}
您也可以select使用索引
e1 = doc.select("tr td:nth-child(2)").text();
e2 = doc.select("tr td:nth-child(3)").text();
e3 = doc.select("tr td:nth-child(4)").text();