【问题标题】:How to parse this specific Data in Jsoup如何在 Jsoup 中解析这个特定的数据
【发布时间】:2014-08-04 18:32:07
【问题描述】:

我不确定如何在 jsoup 中解析“bundesweit”和日期字符串,因为它们的类名相同(col2)

<strong><a href="/stellenangebote/109499-python-entwickler-gui-testing?page=1&amp;query%5Bcity%5D=&amp;query%5Bradius%5D=100&amp;query%5Btext%5D=Python" title="Python-Entwickler (m/w) GUI Testing">Python-Entwickler (m/w) GUI Testing</a></strong>
<br>
<a class="job-offer-teaser-company" href="/unternehmen/ruecker-gmbh" title="Rücker">Rücker</a>
</div>
<div class='col2'>
bundesweit
</div>
<div class='col2'>
08.12.2013
</div>

我试过这个:

Elements jobTitleElement = element.select("a");
                        Elements companyNameElement = element.select(".job-offer-teaser-company");
                        Elements locationElement = element.select(".cal2");

非常感谢

【问题讨论】:

    标签: java html parsing jsoup


    【解决方案1】:

    如果 HTML 遵循相同的结构,只需选择它们,然后使用索引将它们拆分。

    //Get the HTML
    Document doc = Jsoup.parse(html); 
    //or
    Document doc = Jsoup.connect(url).get();
    
    //Select the elements
    Elements col2Elements = doc.select("div.col2"); //This will return a collection of Element objects
    String firstElement = col2Elements.get(0).text(); //Get the first
    String secondElement = col2Elements.get(1).text(); //Get the second
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码:

      Document doc = Jsoup.parse(html);
      Elements elements = doc.getElementsByClass("col2");
      String bundesweitContent = elements.get(0).text();
      System.out.println(bundesweitContent); // You get "bundesweit"
      

      参考:

      http://jsoup.org/cookbook/introduction/parsing-a-document

      http://jsoup.org/apidocs/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多