Jump to content

Recommended Posts

Hey guys,

 

I'm using a script to scrape Google images (http://blogoscoped.com/archive/2007-03-19-n36.html), you can take his source code there to look at it, but the problem is in his code it only displays the first result from the Google images results page -- however, his demo (http://blogoscoped.com/temp/google-image-scraper-2007.html) he setup does the entire first page of Google image results, but he doesn't explain how to do it.

 

I've messed with it alot and can't figure out what is in that code that you could modify to make it display multiple results. I've attached the code below;

 

(btw, this script essentially scrapes from http://images.google.com/images?hl=en&q=horses&btnG=Search+Images&gbv=2 or whatever you input as the variable, [his demo is 'horses'], in case you didn't get what I'm talking about)

 

Thanks guys!

 

<?

$results = getGoogleImages('horses');
foreach ($results as $result) {
    echo '<p><a href="' . htmlentities($result['url']) . '">' .
            '<img src="' . htmlentities($result['thumbnail']) . '" alt="" ' .
            'oncontextmenu="this.src=\'' . htmlentities($result['image']) . '\';return false;" ' .
            'style="border: 1px solid black" /></a><br />' .
            '<em>' . htmlentities($result['description']) . '</em>' .
            '</p>';
}

?>

</body>
</html><?

function getGoogleImages($q, $doSafeSearch = false)
{
    $results = array();

    $safe = ($doSafeSearch) ? 'on' : 'off';
    $url = 'http://images.google.com/images?safe=' . $safe .
            '&q=' . urlencode($q);
    $result = file_get_contents($url);

    $from = 'dyn.Img("';
    $startPos = strPos($result, $from);
    $endPos = strPos($result, ');dyn.updateStatus');
    $functions = substr( $result, $startPos + strlen($from), $endPos );
    $functions = explode('");dyn.Img("', $functions);

    foreach ($functions as $f) {
        $i = count($results);
        list($results[$i]['url'], $v1, $hash,
                $results[$i]['image'],
                $results[$i]['width'], $results[$i]['height'],
                $results[$i]['description'],
                $v2, $v3, $more, $extension, $domain) = explode('","', $f);
        list($results[$i]['url'], $params) = explode('&h', $results[$i]['url']);

        $prefix = 'http://tbn0.google.com/images?q=tbn:';
        $results[$i]['thumbnail'] = $prefix . $hash . ':' . $results[$i]['image'];
        $results[$i]['description'] = strip_tags($results[$i]['description']);
    }

    return $results;
}

?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.