java - Jsoup:如何在迭代期间将 HTML 类组合到产品记录中

标签 java jsoup

我正在尝试抓取这样的菜单:-

<h3 class="product">Four Cheese Spinach Dip</h3>
<h3 class="specialcallout">Vegetarian</h3>
<h4 class="productdescription">The original, made in-house creamy blend
  of four cheeses, spinach, red pepper and onion. Served warm with fried
  pita chips.</h4>
<h4 class="calories"><span class="productprice">11.99</span>
&nbsp;&nbsp;(620 CALS; serves 2)</h4>

我也许可以通过将每个类迭代到字符串数组中来构造一个菜单对象。下面是扫描获取菜单项名称:

boolean show_product(String item){
  Elements elems = doc.getElementsByClass(item);
  System.out.println("Num products " + elems.size() + "\n");
 for (Element el : elems) {
    xprint(" * product: <%s>  (%s)", "_",trim(el.text(), 35));

我必须对产品、价格、描述等有单独的例程吗?

最佳答案

您不必对每个属性都执行此例程,您可以这样做:

boolean show_product() {
    Elements elems = doc.getElementsByClass("product");
    System.out.println("Num products " + elems.size() + "\n");
    for (Element el : elems) {
        System.out.printf(" * %s: <%s>  (%s)\n", el.className(), "_", el.text());
        for (int i = 0; i < 3; i++) {
            Element nextSibling = el.nextElementSibling();
            System.out.printf(" * %s: <%s>  (%s)\n", nextSibling.className(), "_", nextSibling.text());
            el = nextSibling;
        }
        Element spanEl = el.select("span").first();
        System.out.printf(" * %s: <%s>  (%s)\n", spanEl.className(), "_", spanEl.text());
    }
    return true;
}

HTML 类也被组合到产品记录和其他属性中。

关于java - Jsoup:如何在迭代期间将 HTML 类组合到产品记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43837224/

相关文章:

java - 文档返回 null (Android)

java - 从 Jsoup 中的 div 内部获取 <p> 值不起作用

Java 8 stream api 控制输出

java - 如何获取 1983 年 1 月 1 日之后加入的部门员工数量

java - 从我的应用程序拍摄的照片,保存在正确的文件夹中,但在图库中看不到

Java Set - 如何根据名称列表进行排序

java - 如何使用 Java(jsoup 或其他)向 Google 表单提交数据

java - java中的细谷三角

java - 简单的 Java 错误

javascript - 如何在java中使用网络爬虫获取内容