简体   繁体   中英

How to get 'alt' text of an "img" tag of a webpage using JSOUP library in android?

Below is a part of HTML code of a product on amazon webpage. I want to extract the 'alt' text of the 'img' tag on button click in android in which the complete name of product is written. I tried this but it did not worked. Here is a piece of my code by which I was trying to extract the alt text-

Document doc = Jsoup.connect(url).get();
                            Element prodName = doc.select("#image-block img").first();
                            purchaseItem = prodName.attr("alt");

And below is the HTML code of the product page of amazon website. I want to extract the alt text from this HTML code.

<div id="image-block" data-a-carousel-options="{&quot;carouselClass&quot;:&quot;mobile&quot;,&quot;minimum_gutter_width&quot;:0,&quot;show_partial_next&quot;:false,&quot;name&quot;:&quot;image-block-carousel&quot;,&quot;circular&quot;:true}" data-csa-c-content-id="image-block-carousel-mobile" data-csa-c-slot-id="image-block-carousel" data-csa-c-type="widget" data-a-display-strategy="single" data-a-ajax-strategy="none" data-a-class="mobile" class="a-begin a-carousel-container a-carousel-display-single a-carousel-transition-slide a-carousel-initialized" data-csa-c-id="xakgsh-h7d8vs-c9g186-nth6kv"><input autocomplete="on" type="hidden" class="a-carousel-firstvisibleitem"> <div class="a-carousel-viewport a-gesture a-gesture-horizontal" id="anonCarousel1" style="height: 295px;"><ol class="a-carousel" role="list" style="width: 2000px;"><li class="a-carousel-card" role="listitem" aria-setsize="5" aria-posinset="1" aria-hidden="false" style="visibility: visible; width: 400px; margin: 0px;">   <div class="carousel-img-relative-parent aok-inline-block aok-relative">
<span class="a-declarative" data-action="a-secondary-view" data-csa-c-type="widget" data-csa-c-func-deps="aui-da-a-secondary-view" data-a-secondary-view="{&quot;backButtonText&quot;:&quot;Back&quot;,&quot;hideHeader&quot;:true,&quot;name&quot;:&quot;immersive-view&quot;,&quot;scrollable&quot;:true}" data-a-immersive-view="true" data-csa-c-id="t8pgwu-ckc10j-mav7zc-4mnx0c"> <a class="a-link-normal" href="#"> <div class="a-image-wrapper hide-image" data-declarativeactionname="immersive-view" data-imageblockcounter="imageBlockShownC" data-immersiveviewcounter="immersiveViewOpenedC" data-fling-container="true">
<div class="image-wrapper ">
<div class="image-size-wrapper fp-image-wrapper   image-block-display-flex" id="landing-image-wrapper" data-image-height="269" data-image-width="269" data-max-height="200" data-min-height="0" data-num-of-images="5" data-animate-image="0" data-main-image-orientation="" data-image-sizes="[[500, 500], [500, 500], [500, 500], [500, 500], [500, 500]]" data-mainimage-large-pid="31pAe23ncfL" data-mainimage-hires-pid="51UhwaQXCpL" style="height: 290px;">
<img alt="Oneplus Bullets Z2 Bluetooth Wireless in Ear Earphones with Mic, Bombastic Bass - 12.4 Mm Drivers, 10 Mins Charge - 20 Hrs Music, 30 Hrs Battery Life, Launched in April 2022 (Magico Black)" src="https://m.media-amazon.com/images/I/31pAe23ncfL._AC_SY580_FMwebp_.jpg" data-fling-asin="B09TVVGXWS" data-fling-refmarker="detail_main_image_block" data-midres-replacement="https://m.media-amazon.com/images/I/31pAe23ncfL._AC_SY350_FMwebp_.jpg" data-zoom-hires="https://m.media-amazon.com/images/I/51UhwaQXCpL._AC_SL1500_.jpg" onload="setCSMReq('af');addlongPoleTag('af','awudplpimage');;if(typeof uet === 'function'){uet('x2');} markFeatureRenderForImageBlock(); aibEnlargeIB(document)" onerror="setCSMReq('af');" class="imageLeft0 altImage visibility-hidden" id="main-image" data-a-image-name="mainImage" height="px" width="px" data-a-hires="https://m.media-amazon.com/images/I/31pAe23ncfL._AC_SY580_FMwebp_.jpg" style="height: 290px; width: 261px; visibility: visible;">    

If I am understanding correctly you are trying to grab the alt tag from the last img:

Element prodName = doc.select("img#main-image").first();
purchaseItem = prodName.attr("alt");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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