我正在尝试抓取这样的菜单:-
<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>
(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/