我正在使用jsoup从网站中的表中提取数据。http://www.moneycontrol.com/stocks/marketstats/gainerloser.php?optex=BSE&opttopic=topgainers&index=-1使用Jsoup。我提到了Using JSoup To Extract HTML Table Contents和其他类似的问题,但它没有打印数据。有人可以请我提供实现此目的所需的代码吗?
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());
}
}
}
答案 0 :(得分:1)
如果您想获得表格的内容(而不是头部),则需要更改表格的选择器:
for (Element table : doc.select("table.tbldata14"))
而不是
for (Element table : doc.select("table.tablehead"))
答案 1 :(得分:0)
一个重要的事情是在解析HTML时检查你在Doc中得到了什么,因为它可能没有什么问题,例如: 1.本网站可能正在使用iframe来显示内容 2.通过Javascript显示内容 3.很少有站点具有不允许jsoup解析的脚本,因此doc元素将包含随机数据