Ammar Tareen
Ammar Tareen

Reputation: 51

JSoup fetching image issue

I'm trying to fetch an image into my android app but I am unable do so after several failed attempts. I would like to get the image for current weather icon the page "http://www.wunderground.com/US/ma/boston.html?MR=1"

It's the icon right next to temperature and it's html code looks like:

            <img src="http://icons-ak.wxug.com/i/c/k/nt_partlycloudy.gif" width="44" height="44" alt="Scattered Clouds" class="condIcon">

I use code that looks like as follows but I am unsure on what to pass as the argument of the doc.select(" ... "); statement in the second line. When I use the following code exactly, I can retrieve the image from the androidbegin.com website. Please help figure out what argument to use for the wunderground link above.

        Document doc = Jsoup.connect("http://www.androidbegin.com").get();        
            Elements img = doc.select("h1[class=image-logo] img[src]");
            String imgSrc = img.attr("src");
            InputStream input = new java.net.URL(imgSrc).openStream();
            bitmap = BitmapFactory.decodeStream(input);

So far I have used lines like:

Elements img = document.select("curIcon[class=condIcon] img[src]");

but to no avail. Alternatively, please suggest a resource where I could learn about these arguments myself.

Thank you.

Upvotes: 1

Views: 372

Answers (1)

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

I believe your selector is not right. Try this

Elements img = document.select("#curIcon img[src]");

These are css selectors. So you can learn about them here and here

I believe this would work too

Element image = document.select("#curIcon img").first();
String url = image.absUrl("src");

Upvotes: 1

Related Questions