【问题标题】:Trying to Extract Info from a Website but some values appear as null?尝试从网站中提取信息但某些值显示为空?
【发布时间】:2016-05-30 16:27:22
【问题描述】:

我已经使用 JSoup 工作和测试了一段时间,这个问题困扰了我一段时间。

http://fx.sauder.ubc.ca/today.html

目前这个类应该从这个网站的表格中提取信息,这个程序可以从表格中提取的唯一东西是

Code    |    Currency   |    fcu/CAD    |    fcu/USD    |    Code   |    Currency   |    fcu/CAD    |    fcu/USD 

以及网站上显示的所有 3 个字母代码,但对于所有其他信息,例如值和美元名称,程序将这些信息显示为空。如果有人想知道 px 上升到 34,ay 上升到 16,因为这就是我要从中提取的表格的大小。

public String CountryHandler2(int px, int ay) throws IOException{
    String url = "http://fx.sauder.ubc.ca/today.html";
    Document doc = Jsoup.connect(url).get();
    Elements paragraphs = doc.select("body > table:nth-child(4) > tbody:nth-child(1) > tr:nth-child("+px+") > td:nth-child("+ay+") > font:nth-child(1) > b:nth-child(1)");

    System.out.println("Paragraphs " + paragraphs.text());
    if(paragraphs.hasText()){  
        return paragraphs.text();
    }
    return null;
}

【问题讨论】:

标签: java html jsoup


【解决方案1】:

我试过这个并且能够提取表格数据。或许你可以这样尝试。

    public static void main (String [] args) throws IOException{
       Document doc = Jsoup.connect("http://fx.sauder.ubc.ca/today.html").get();
       Element table = doc.select("table").get(2); //get the 3rd table
       Elements trs = table.select("tr"); //get each row of that table
       for (Element e: trs){
          System.out.println(e.text());
       }
    }

【讨论】:

    猜你喜欢
    • 2010-09-24
    • 2018-06-18
    • 1970-01-01
    • 2016-04-18
    • 2013-03-21
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多