0

I am trying to fetch images from url whose width and height is >249 I tried to fetch image from this url http://imgur.com/gallery/zwxxH8T 'http://www.wallpaper.com/' from few url it is fetching images, but why this is not getting image from all given url.

I am using following code

public static void main(String[] args) {
        try {
            String path = "http://imgur.com/gallery/zwxxH8T";
            System.out.println("Fetching %s..." + path);
            try {
                URL url = new URL(path);
            } catch (MalformedURLException e) {
                System.out.println("MalformedURLException");

            }
            Document doc = Jsoup.connect(path).timeout(5000).get();
            Elements media = doc.select("[src]");
            int width = 0;
            int height = 0;
            for (Element src : media) {
                if (src.tagName().equals("img")) {
                    try {
                        width = Integer.parseInt(src.attr("width"));
                        height = Integer.parseInt(src.attr("height"));
                    } catch (NumberFormatException ex) {
                    }
                    if ((width > 249) && (height > 249)) {
                        System.out.println("Path:  " + src.attr("abs:src")
                                + "\n wd " + src.attr("width") + " hi " + src.attr("height"));

                    }
                }
            }
        } catch (org.jsoup.UnsupportedMimeTypeException e) {
            System.out.println("Exception " + e);

        } catch (IOException e) {
            System.out.println("Exception " + e);
        }
    }
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

2 Answers2

1

In the given url https://i.stack.imgur.com/MgQWo.jpg, no <img> contains width and height attributes.

So width and height always equal 0, if ((width > 249) && (height > 249)) is always false, no image is found.

ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • So how can we get actual width and height of image – xrcwrn Oct 08 '14 at 09:54
  • You have to download it first. Then, here are some solutions to get what you want : http://stackoverflow.com/questions/672916/how-to-get-image-height-and-width-using-java – ToYonos Oct 08 '14 at 09:58
0

If you just want to get the src to the main image, rather than looking for a specific size (given that they're not setting one), you can make a CSS like div.image img. This looks for <div class="image"><img ...>, which matches their maing image.

You also don't need all that loop code.

Document doc = Jsoup.get(url).timeout(t).get();
Element image = doc.select("div.image img").first();
String source = image.attr("abs:src");

Example at http://try.jsoup.org/~YV4U6fB6yeR2eScoXgjzKjynxh0

Jonathan Hedley
  • 10,442
  • 3
  • 36
  • 47