namarino namarino - 3 years ago 283
Java Question

Java Jsoup Google Image Search result parsing

I'm using jsoup to parse Google image results. I'm trying to get the

of the image. Here is my code so far. The output is truncated for some reason and I can't access the
attribute. Does anyone know why this is happening and what I can do to fix it? Thanks so much!

public static void main(String args[]) {
try {
// Does a google image search for "test"
final Document doc = Jsoup.connect("").userAgent(USER_AGENT).get();

// selects images
Elements elements ="img.rg_ic.rg_i");
// cycles through elements and prints attributes
for (Element e : elements) {

} catch (IOException e) {


<img class="rg_ic rg_i" data-sz="f" name="XWXPqrX1RFJiaM:" alt="Image result for test" jsaction="load:str.tbn" onload="google.aft&&google.aft(this)">

Answer Source

The following code provides the urls for the first 100 image results with jsoup. If you need all results you have to use a headless browser (I recommend PhantomJS, see this answer for usage).

The static html source has the image urls for the first 100 result solely stored in JSON objects. For parsing the scraped JSON objects, I used JSON.simple

The JSON objects are contained in the <div> elements with class rg_meta and are in the following form:

{"st":"Uber","tu":"https:\/\/\/images?q=tbn:ANd9GcTSEUMluu1kigjR3JU40BYfaH0fQ6JW1vk9WScBiXr--lsMILf2","ru":"https:\/\/\/uberkittens-are-back\/","tw":300,"pt":"UberKittens Delivers Kittens to Play or Stay","ou":"https:\/\/\/wp-content\/uploads\/2015\/10\/HQ_uberkittens_blog_960x540_r1v1.jpg","ow":960,"cl":6,"isu":"","rid":"vLA3QXY8xPE4PM","cr":3,"ity":"jpg","sc":1,"ct":15,"s":"Clear Your Calendars\u2014#UberKITTENS Are Back","th":168,"oh":540,"id":"qCR7qXt7VX38iM:","itg":false,"cb":15}

So for the url we need to extract the value for the key "ou".

Example Code

// can only grab first 100 results
String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
String url = "";

List<String> resultUrls = new ArrayList<String>();

try {
    Document doc = Jsoup.connect(url).userAgent(userAgent).referrer("").get();

    Elements elements ="div.rg_meta");

    JSONObject jsonObject;
    for (Element element : elements) {
        if (element.childNodeSize() > 0) {
            jsonObject = (JSONObject) new JSONParser().parse(element.childNode(0).toString());
            resultUrls.add((String) jsonObject.get("ou"));

    System.out.println("number of results: " + resultUrls.size());

    for (String imageUrl : resultUrls) {

} catch (IOException | ParseException e) {


number of results: 100 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download