java - JSOUP .attr() 方法不从工作 html 中提取数据

标签 java jsoup

我的 .attr 方法存在问题,该方法不适用于除“class”之外的所有属性。 我尝试提取“alt”属性来获取商店的名称,但它不起作用。对“src”和“data-original”进行了相同的尝试,但没有打印出任何内容。

这是我用来提取数据的完整方法。

public List<String> getShops() {
        Elements elements = document.select(".store-logo");
        System.out.println(elements.html());
       for(Element image : elements){
           System.out.println(image.attr("alt"));
       }
        return null;
    }

为了确保我没有使用空文档,我为所有看起来像这样的元素打印了整个 HTML:

<img src="//image.ceneostatic.pl/imageschain/data/shops_s/20853/logo.jpg;data/custom_images/590/custom_image.png" alt="nalepsze.pl">
<img src="/content/img/icons/pix-empty.png" alt="allegro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/20136/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="avans.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/18601/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="proshop.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/29068/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="g2a.com" data-original="//image.ceneostatic.pl/imageschain/data/shops/23040/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="fotosoft.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/3914/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="techsat24.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/5666/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="imperiumpc.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/12579/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="domsary.eu" data-original="//image.ceneostatic.pl/imageschain/data/shops/4725/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="net-s.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/3653/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="sferis.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/4614/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="morele.net" data-original="//image.ceneostatic.pl/imageschain/data/shops/379/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="zakupy.vip" data-original="//image.ceneostatic.pl/imageschain/data/shops/29402/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="fotoelektro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/1671/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="3kropki.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/357/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="electro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/16202/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="allegro.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/20136/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="avans.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/18601/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">
<img src="/content/img/icons/pix-empty.png" alt="proshop.pl" data-original="//image.ceneostatic.pl/imageschain/data/shops/29068/logo.jpg;data/custom_images/585/custom_image.png" class="js_lazy">

此提取中的数据是正确的,但每个循环中的下一步不起作用,我为每个元素得到一个空字符串,这很奇怪,因为我可以提取“类”属性。

我非常感谢根据该主题提供的任何提示。

PS。 jsoup 版本是 1.11.3

最佳答案

在您的示例中,您使用类“store-logo”,但在附加的 html 文档中,没有一个 img 元素具有此类。将类名替换为“js_lazy”时,您的代码会提取 alt 属性。

关于java - JSOUP .attr() 方法不从工作 html 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60221941/

相关文章:

java - Spring Boot 防止 "Port In use"

java - 用JSOUP解析HTML文档,无法选择表格节点?

java - 无法获取img标签

Java jsoup - 清除除链接以外的所有标签

java - 想使用 Jsoup 获取 Title 和 Grade

java - DELETE 方法无法正确删除实体

java - Eclipse 火星 - eclipse.org.equinox.p2.core.provisionexception

java - 使用 Jackson 将 json 数组转换为数组中具有不同对象元素的 java 对象

java - 文件上传 JSP 和 Servlet 2.5 未获取 Textarea 值

java - 为什么使用 Jsoup 连接到特定网站时会收到 EOFException?