【问题标题】:Jsoup ArrayList and LinkedHashMap ComboJsoup ArrayList 和 LinkedHashMap 组合
【发布时间】:2015-06-03 19:17:28
【问题描述】:

我编写的代码使用 jsoup 访问网站,查看所有段落标题,然后将它们保存到名为 headingList 的 ArrayList 中。这是棘手的部分。我有一个将字符串作为键和 ArrayLists 作为值的映射。该代码的设计方式要求它进入多个页面。因此,标题数量以及与标题相关的段落数量可能会有很大差异。所以,这里的想法是创建两个 int 值。一个名为headingAmt 的int 值在它查看页面并确定有多少标题后设置。第二个名为headCount 的int 值被初始化为1。然后我要做的是设置一个像这样的while 循环:while(headCount != headAmt + 1) 并在循环结束时递增它,以便它在@ 时终止987654325@ 遍历每个标题。在 while 循环中,我尝试将每个段落添加到名为 items 的 ArrayList 中,然后获取 items 数组列表中的内容,然后将其设置为地图中第一项的值。然后,清除 ArrayList,转到下一段,将那里的内容保存到 items,然后将该 ArrayList 设置为地图中第二项的值,依此类推。我有我可以发布的代码,但是由于我无法让它正常工作,所以有问题的 while 循环已经重新排列了很多次,这很令人困惑。

如果有人可以提供帮助,请编辑以下代码:

public class Finder {

public Finder(String url) {
    String mainURL = "http://www.website.com";
    Map<String, List<String> > headMap  = new HashMap<>();
    ArrayList<String> headingList = new ArrayList<>();
    ArrayList<String> items = new ArrayList<>();
    int headCounter = 1;

    String itemList = "div > div:nth-child(1).category > ul:nth-child(2) > li.item > span";
    int headAmt;



    Document doc1 = null;


    ///// Connect to site to get menu /////
    try{
        doc1 = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
                .referrer("http://www.google.com")
                .get();
    }
    catch(IOException e){
        System.out.println("Can't connect to website");
    }

        /////// Get headings ////////
        Elements head = doc1.select("div > div > div > h3");

        ////// Loop through headings and add to ArrayList /////
        for(Element e: head){
            headingList.add(e.text());

        }
        headAmt = headingList.size();

        /*
        Here is the problem
         */

        while(headCounter != headAmt + 1){

            Elements elem = doc1.select("div > div:nth-child("+ headCounter +").category > ul:nth-child(2) > li.item > span");


            for (String key : headingList) {
                for(Element e : elem){
                items.add(e.text());
                }

                List<String> value = new ArrayList<>(items);
                headMap.put(key, value);
            }

            items.clear();
            headCounter++;
            }
            }
        }
    }
}

【问题讨论】:

    标签: java android arraylist linkedhashmap


    【解决方案1】:

    你可以试试这样的:

    public class Finder {
    public static void main(String[] args) {
        new Finder(
                "http://www.allmenus.com/ny/new-york/250087-forlinis-restaurant/menu/");
    }
    
    public Finder(String url) {
        Document doc1 = null;
        try {
            doc1 = Jsoup
                    .connect(url)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
                    .referrer("http://www.google.com").get();
        } catch (IOException e) {
            System.out.println("Can't connect to website");
        }
        Elements elements = doc1.select(".category");
        HashMap<String, ArrayList<List<String>>> menu = new HashMap<String, ArrayList<List<String>>>();
        for (Element e : elements) {
            String name = e.select(".category_head>h3").first().text();
            Elements itms = e.select("ul > li");
            ArrayList<List<String>> menuItems = new ArrayList<List<String>>();
            for (Element it : itms) {
                menuItems.add(Arrays.asList(new String[] {
                        it.select("span").first().text(),
                        it.select("span").eq(1).text() }));
            }
            menu.put(name, menuItems);
    
        }
        for (String key : menu.keySet()) {
            System.out.println(key);
            ArrayList<List<String>> lst = menu.get(key);
            for (List<String> item : lst) {
                System.out.println("       " + item.get(0) + " " + item.get(1));
            }
            System.out.println("\n");
        }
    }
    }
    

    【讨论】:

    • 我发布了代码 - 不要被最后一个 while 循环挂断。我知道这是错误的,我只是需要帮助解决它
    • 对不起,我将代码恢复到原来的状态,但它仍然无法正常工作。任何帮助表示赞赏。我在这里拉头发。
    • hmmm 现在当我打印出地图时,值显示为空数组列表。我应该更新我帖子中的代码以显示我现在是如何得到它的吗?
    • @GD 这可能是因为select(...) 方法不返回任何元素。
    • @GD 是由额外循环引起的 for (String key : headingList)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多