繁体   English   中英

如何使用jsoup顺序提取数据

[英]How to extract data in sequence using jsoup

我正在尝试使用jsoup从此链接https://orderup.com/some/phoenix/delivery/featured获取数据,但是我遇到了一些问题,即我的结果数据格式不正确,并且具有描述的类别也未显示。 这是我的代码:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class grabber {
     public static void main(String[] args) throws Exception {
            String url = "https://orderup.com/restaurants/bella-pizza-r3834/delivery";
            Document document = Jsoup.connect(url).get();
            Elements restname = document.select("h1.urbana");
            System.out.println("restname: " + restname.text());
            Elements restaddressdiv = document.select("address.desktop-address");
            Elements restauranthours = document.select("div.restaurant-hours-region");
            Elements restauranthoursa = restauranthours.select("div.restaurant-hours-region");
            Elements restauranthoursregion = restauranthoursa.select("dt");
            System.out.println("restauranthosssurs: " + restauranthoursregion.size());
            for (Element resthours : restauranthoursregion) {
                System.out.println("restauranthours: " + resthours.text());
            }
            Elements h3 = document.select("div.menu-category");
            Elements h3tag = h3.select("h3");
            for(Element e : h3tag)
            {
                 System.out.println("Category: " + e.text());  

                 if (e.nextElementSibling().select("p").size() == 1) {
                     Elements itemtitlep =e.nextElementSibling().select("p");
                     Elements itemtitle = e.nextElementSibling().select("span.item-title");
                     System.out.println(itemtitle.size());
                        int itemtitleCount = itemtitle.size();
                        System.out.println("ifffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff statement");
                        for(Element itema : itemtitle)
                        {
                            System.out.println("Items: " + itema.text());
                            Elements itemtitleprice = itema.nextElementSibling().select(".item-price");
                            Elements itemtitledes = itema.getElementsByTag("p");
                            for(Element itempricea : itemtitleprice)
                            {
                                System.out.println("price: " + itempricea.text());
                            }
                            for(Element itemdesc : itemtitledes)
                            {
                                System.out.println("itemdesc: " + itemdesc.text());
                            }
                        }
                } else {
                    Elements itemtitle = e.nextElementSibling().select("span.item-title");
                    int itemtitleCount = itemtitle.size();
                    System.out.println(itemtitleCount);
                    System.out.println("elssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss statement");
                    for(Element itema : itemtitle)
                    {
                        System.out.println("Items: " + itema.text());
                        Elements itemtitleprice = itema.nextElementSibling().select(".item-price");
                        Elements itemtitledes = itema.getElementsByTag("p");
                        for(Element itempricea : itemtitleprice)
                        {
                            System.out.println("price: " + itempricea.text());
                        }
                        for(Element itemdesc : itemtitledes)
                        {
                            System.out.println("itemdesc: " + itemdesc.text());

                        }
                    }
                }
            }
        }
}

问题是,您访问的页面的html比您预期的要灵活一些。 例如,在某些类别中,您有一个主类别的副文本。 这被组织为h3标签的下一个兄弟。 一种更健壮,也更易于阅读的方法可能是这样的:

Elements elh3s = document.select("div.menu-category h3");
for (Element elh3 : elh3s){
    System.out.println("Category: " + elh3.text());

    //get the list by stepping up and then css select the ul
    Elements ellis = elh3.parent().select("ul>li");
    for (Element elli : ellis){
        System.out.println("title: " 
            + elli.select("span.item-title").first().text());
        System.out.println("price: " 
            + elli.select("span.item-price").first().text());
        System.out.println("--");
    }
}

建议:

查看Jsoup CSS选择器 它们非常强大,并且由于您已经用JSoup解析了页面,因此可以在几乎没有性能问题的情况下充分使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM