首页 > 解决方案 > 如何使用 Jsoup 库获取对象的所有详细信息并将其保存到 bean?

问题描述

在这里,我正在抓取网站https://hamrobazaar.com/c6-apparels-and-accessories,我想将所有子类别的详细信息存储在一个 bean 中并打印它们。如果我得到相应的对象的详细信息,那么这也会有很大帮助。

例子:

图片来自同一站点,即 https://hamrobazaar.com/c6-apparels-and-accessories

我想将口罩的名称刮掉为 Kn95 口罩(fda 认证),描述为我们是卖家...,卖家名称为 Birodh Pokhrel,地址为 Damak-5,Damak,价格为 210,日期和类型为全新

如果你擅长 Jsoup 和 Xpath。请帮我获得这个。谢谢你

标签: javahtmlxpathjsouphtml-parsing

解决方案


对于 XPath 部分(jsoup不支持它,所以也许你可以试试xsoup):

一些选择器从广告中获取详细信息,包括黄色背景的选择器,每个页面都保持相同。(文章标题、描述、卖家、地址、价格、物品状况):

//font[@style]/b
//b[.="Seller:"]/preceding-sibling::text()[normalize-space()]
//b[.="Seller:"]/following-sibling::a
//b[.="Seller:"]/following-sibling::font
//b[starts-with(.,"Rs.")]
//b[starts-with(.,"Rs.")]/following-sibling::font

每个细节的元素数量:21

一些选择器从广告中获取详细信息,不包括黄色背景的选择器,每个页面都保持相同。(文章标题、描述、卖家、地址、价格、物品状况):

//font[@style][not(ancestor::table[@id])]/b
//b[.="Seller:"][not(ancestor::table[@id])]/preceding-sibling::text()[normalize-space()]
//b[.="Seller:"][not(ancestor::table[@id])]/following-sibling::a
//b[.="Seller:"][not(ancestor::table[@id])]/following-sibling::font
//b[not(ancestor::table[@id])][starts-with(.,"Rs.")]
//b[not(ancestor::table[@id])][starts-with(.,"Rs.")]/following-sibling::font

每个细节的元素数量:20

旁注:小心物品状况。某些广告缺少此字段。因此,元素的数量可能低于 20 或 21。


推荐阅读