从Google Image获取结果时如何获得超过20张图片?

时间:2012-08-12 08:52:39

标签: php curl

下面的脚本从Google获取图片,它只获得$ page变量中指定页面的20张图片。

我没弄清楚为什么它会得到20个结果,如何将此值更大,以显示100个第一个图像,例如

<?php


// Image sizes
define ('GIS_LARGE', 'l');
define ('GIS_MEDIUM', 'm');
define ('GIS_ICON', 'i');
define ('GIS_ANY', '');

// Image types
define ('GIS_FACE', 'face');
define ('GIS_PHOTO', 'photo');
define ('GIS_CLIPART', 'clipart');
define ('GIS_LINEART', 'lineart');

function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}


function googleImageSearch ($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY)
{

$retVal = array();

// Get the search results page


$response = get_data("http://images.google.com/images?hl=en&q=" . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21));

// Extract the image information. This is found inside of a javascript call to setResults
preg_match('/\<table class=\"images_table\"(.*?)\>(.*?)\<\/table\>/is', $response, $match);

if (isset($match[2])) {

    // Grab all the arrays
    preg_match_all('/\<td(.*?)\>(.*?)\<\/td\>/', $match[2], $m);

    foreach ($m[2] as $item) {

        // List of expressions used to grab all our info
        $info = array(
            'resultLink' => '\<a href=\"(.*?)\"',
            'source' => 'imgurl=(.*?)&amp;',
            'title' => '\<br\/\>(.*?)\<br\/\>([\d]+)',
            'width' => '([\d]+) &times;',
            'height' => '&times; ([\d]+)',
            'type' => '&nbsp;-([\w]+)',
            'size' => ' - ([\d]+)',
            'thumbsrc' => 'src="(.*?)"',
            'thumbwidth' => 'width="([\d]+)"',
            'thumbheight' => 'height="([\d]+)"',
            'domain' => '\<cite title="(.*?)"\>'
        );

        $t = new stdClass;
        $t->thumb = new stdClass;
        foreach ($info as $prop => $expr) {
            if (preg_match('/' . $expr . '/is', $item, $m)) {
                $value = 'title' == $prop ? str_replace(array('<b>', '</b>'), '', $m[1]) : $m[1];

                // Thumb properties go under the thumb object
                if (0 === strpos($prop, 'thumb')) {
                    $prop = str_replace('thumb', '', $prop);
                    $t->thumb->$prop = $value;
                } else {
                    $t->$prop = $value;
                }

                // Nicey up the google images result url
                if ('resultLink' == 'resultLink') {
                    $t->resultLink = 'http://images.google.com' . $t->resultLink;
                }

            }
        }

        $retVal[] = $t;

    }

}

return $retVal;

}

告诉脚本获取20张图像的代码行在哪里?

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

嗯,你不能。该脚本从标准版Google图像中获取结果,并且无法更改每页的结果。你唯一能做的就是要求五次拥有100张图像。

更新:要继续更新附加图片,请使用“+”运算符。像,

$image = array();

for( $i = 1; $i <= 5; $i++ )
     $image += googleImageSearch ($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY);

请注意,如果您不聪明地隐瞒您的请求,或谷歌对自动请求有疑问,您可能会遇到此页面。


enter image description here

答案 1 :(得分:0)

您应该使用google API。

https://developers.google.com/custom-search/v1/overview

正如@shubham所提到的,当Google检查机器人时,你肯定会在这个页面上结束。