如何解决使用 JSoup 从网站收集列表条目时被抛出我的 for 循环
我的目标是使用 JSoup 从食谱页面中提取成分列表。 我设法从网站上获得了我的第一个列表条目,但是我的 for 循环似乎在第一个条目处停止,而没有收集下一个 5。
我不确定我做错了什么,所以如果你能看看我的代码,我将不胜感激:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class WebScrape {
public static void main(String[] args) {
scrapeBBC("https://www.bbcgoodfood.com/recipes/spanish-omelette");
}
static void scrapeBBC(String url){
try{
Document recipe = Jsoup.connect(url).get();
for(Element ingredients : recipe.select("section.recipe__ingredients.col-12.mt-md.col-lg-6")){
//TODO: if problems occur with null entries add if-else as suggested in the video
int row = 0;
final String ingredient = ingredients.select(
"li.list-item--separator.list-item.pt-xxs.pb-xxs:nth-of-type("+ row++ +")").text();
System.out.println(row + ingredients.select(
"li.list-item--separator.list-item.pt-xxs.pb-xxs:nth-of-type("+ row++ +")").text());
//System.out.println(row + ingredient);
}
}catch(IOException ioe){
System.out.println("Unable to connect to the URL.");
ioe.printStackTrace();
}
}
}
提前致谢!
解决方法
首先选择 ingredients
部分。
Element ingredients = recipe.select("section.recipe__ingredients.col-12.mt-md.col-lg-6").first();
然后遍历该部分中存在的 <li>
元素。
int row = 0;
for (Element ingredient : ingredients.select("li.list-item--separator.list-item.pt-xxs.pb-xxs")) {
System.out.println(++row + " : " + ingredient.text());
}
顺便说一句,您的选择器不必非常具体;以下选择器可以正常工作。
recipe.select("section.recipe__ingredients")
ingredients.select("li")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。