我有一个网上商店,在搜索某些项目后,我需要从中获得一组具有特定参数的产品,如名称,价格,描述等。通过关键字输入执行搜索。 这是一个网站:https://www.aboutyou.de/dein-shop。这是我试图解析的搜索:https://www.aboutyou.de/frauen/accessoires/huete-und-muetzen/caps?pl=1
因此,我需要获取所有这些数据,因为在页面的html源代码中存在复杂的标记层次结构。我试图通过类获取我的元素,除了最高的一个由id标记并由body包裹。 所以我试着用jsoup做这样的事情:
lements resultt = doc.body().select("main#app");
for(Element el : resultt){
Elements main = el.getElementsByClass("section.layout_11glwo1-o_O-stretchLayout_1jug6qr > " +
"div.content_1jug6qr > " +
"div.container > " +
"div.mainContent_10ejhcu > " +
"div.productStream_6k751k > " +
"div > " +
"div.wrapper_8yay2a > " +
"div.col-sm-6.col-md-4 > " +
"div.wrapper_1eu800j > " +
"div > " +
"div.categoryTileWrapper_e296pg > " +
"a.anchor_wgmchy > " +
"div.details_197iil9 > " +
"div.meta_1ihynio > " +
"div.finalPrice_11ythok > " +
"span.price_1543wg1");
System.out.println("just print it" + main.text());
}
但是“打印它”之后没有打印出来。我知道这不是一个配置问题因为我使用更高级别的标签绑定了一些信息。我可以在代码中获取有关此产品实体信息的信息吗? 我是jsoup的新手,所以我会感激任何帮助!
答案 0 :(得分:1)
如果您将Elements main = el.getElementsByClass("section.layout_11glwo1-o_O-stretchLayout_1jug6qr >...
替换为el.select
,则会获得所有价格的列表:
17,90€19,90€19,90€24,90€19,90€24,90€24,90€19,90€17,90€19,90€17,90€49,90 €39,90€39,90€17,90€19,99€39,90€19,99€34,95€17,90€
这可能是因为选择器不是类选择器。
修改强> 这是适合我的完整代码 -
Document doc = Jsoup.connect("https://www.aboutyou.de/frauen/accessoires/huete-und-muetzen/caps?pl=1")
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0")
.get();
Elements result = doc.body().select("main#app");
for(Element el : result) {
Elements e = el.select("section.layout_11glwo1-o_O-stretchLayout_1jug6qr > " +
"div.content_1jug6qr > " +
"div.container > " +
"div.mainContent_10ejhcu > " +
"div.productStream_6k751k > " +
"div > " +
"div.wrapper_8yay2a > " +
"div.col-sm-6.col-md-4 > " +
"div.wrapper_1eu800j > " +
"div > " +
"div.categoryTileWrapper_e296pg > " +
"a.anchor_wgmchy > " +
"div.details_197iil9 > " +
"div.meta_1ihynio > " +
"div.finalPrice_11ythok > " +
"span.price_1543wg1");
System.out.println(e.text());
}