【问题标题】:Get thrown out of my for loop when gathering list entries from website using JSoup使用 JSoup 从网站收集列表条目时被抛出我的 for 循环
【发布时间】:2021-08-01 17:09:26
【问题描述】:

我的目标是使用 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();
        }
    }
}

提前致谢!

【问题讨论】:

    标签: java web-scraping jsoup


    【解决方案1】:

    首先选择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")
    

    【讨论】:

    • 工作就像一个魅力!感谢您的帮助和建议。
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多