使用jSoup从Java中的网页中提取所有图像替代文本

时间:2019-04-19 13:39:15

标签: java jsoup

我想为网页中的所有图像提取替代文本。 我知道我尝试了所有方法。你可以检查一下 下面的代码。

Document doc = Jsoup.connect("https://www.amazon.com/gp/offer-listing/B003FYLW9Q/ref=olp_f_new?ie=UTF8&f_new=true")
    .userAgent("Mozilla")
    .timeout(50000)
    .cookie("cookiename", "val234")
    .cookie("anothercookie", "ilovejsoup")
    .referrer("http://google.com")
    .header("headersecurity", "xyz123")
    .get();


// Method 1
Elements images = doc.select("img[src~=(?i)\\.(gif)]");
System.out.println(images.attr("alt"));  

// Method 2
String imageAlt = doc.getElementsByClass("a-spacing-none olpSellerName").select("img").attr("alt");
System.out.println(imageAlt);  

现在,此代码现在在connect方法中不适用于链接。 它不适用于某些链接,也无法获取该网页中的所有URL。

但这适用于以下链接:

https://www.amazon.com/gp/offer-listing/B06XWZWYVP/ref=olp_f_new?ie=UTF8&f_new=true

https://www.amazon.com/gp/offer-listing/B079JD7F7G/ref=olp_f_new?ie=UTF8&f_new=true

所有链接的类均相同。但这不适用于某些链接。谁能告诉我解决这个问题的方法。

0 个答案:

没有答案
相关问题