【问题标题】:JSoup parsing data from within a tagJSoup 从标签内解析数据
【发布时间】:2014-05-31 01:36:33
【问题描述】:

我正在设法解析我需要的大部分数据,除了一个,因为它包含在 a href 标记中,我需要出现在 "mmsi="

之后的数字
<a href="/showship.php?mmsi=235083844">Sunsail 4013</a>

我当前的解析器获取我需要的所有其他数据并且在下面。我尝试了一些东西,注释掉的代码偶尔会返回未指定的条目。有什么方法可以添加到下面的代码中,以便在返回数据时在名称“Sunsail 4013”之前返回数字“235083844”?

try {
        File input = new File("shipMove.txt");
        Document doc = Jsoup.parse(input, null);
        Elements tables = doc.select("table.shipInfo");
        for( Element element : tables )
        {
            Elements tdTags = element.select("td");
            //Elements mmsi = element.select("a[href*=/showship.php?mmsi=]");
            // Iterate over all 'td' tags found
            for( Element td : tdTags ){
                // Print it's text if not empty
                final String text = td.text();
                if( text.isEmpty() == false )
                {
                    System.out.println(td.text());
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

数据解析示例和html文件here

【问题讨论】:

    标签: java parsing jsoup


    【解决方案1】:
    1. 您可以在Element 对象上使用attr 来检索特定属性的值
    2. 如果字符串模式一致,使用substring获取所需的值

    代码

    // Using just your anchor html tag
    String html = "<a href=\"/showship.php?mmsi=235083844\">Sunsail 4013</a>";
    Document doc = Jsoup.parse(html);
    
    // Just selecting the anchor tag, for your implementation use a generic one
    Element link = doc.select("a").first();
    
    // Get the attribute value
    String url = link.attr("href");
    
    // Check for nulls here and take the substring from '=' onwards
    String id = url.substring(url.indexOf('=') + 1);
    System.out.println(id + " "+ link.text());
    

    给予,

    235083844 Sunsail 4013
    

    在您的代码中修改for 循环中的条件:

    ...
        for (Element td : tdTags) {
                    // Print it's text if not empty
                    final String text = td.text();
                    if (text.isEmpty() == false) {
                        if (td.getElementsByTag("a").first() != null) {
                            // Get the attribute value
                            String url = td.getElementsByTag("a").first().attr("href");
    
                            // Check for nulls here and take the substring from '=' onwards
                            String id = url.substring(url.indexOf('=') + 1);
                            System.out.println(id + " "+ td.text());
                        }
                        else {
                            System.out.println(td.text());
                        }
                    }
                }
    ...
    

    以上代码将打印所需的输出。

    【讨论】:

    • 抱歉,我真的不确定如何将它添加到我当前的代码中。除非我更改 Element link = element.select("a").first();然后它在我的其他结果之前被打印速度搞砸了。我将编辑我的问题以显示文本文件以及我当前的代码返回的内容。
    • 非常感谢,它运行良好。当我添加 attr 元素时,我在第一个 for 循环中执行它,这就是它不起作用的原因。
    【解决方案2】:

    如果你需要属性的值,你应该使用attr()方法。

    for( Element td : tdTags ){
        Elements aList = td.select("a");
        for(Element a : aList){
            String val = a.attr("href");
            if(StringUrils.isNotBlank(val)){
                String yourId = val.substring(val.indexOf("=") + 1);
    
    
            }
    }
    

    【讨论】:

    • 是StringUtils吗?如果有任何机会,您可以解释我如何将其包含在我当前的代码中,因为我仍然需要我正在检索的其余 td 元素。
    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2016-11-03
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多