簡體   English   中英

如何使用php腳本下載Google搜索大圖?

[英]How to download google search big image using php script?

我想下載Google搜索大圖。 以下是我的代碼,可以很好地從Google搜索下載小圖片。

<?php
include_once('simple_html_dom.php');
set_time_limit(0);
$fp = fopen('csv/search.csv','r') or die("can't open file");
$csv_data = array();
while($csv_line = fgetcsv($fp)) {
   for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
    $imgname = $csv_line[$i];
    $search_query = $csv_line[$i];
    $search_query = urlencode(trim($search_query));
    $html=file_get_html('http://images.google.com/images?as_q='. $search_query .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y');
    $image_container = $html->find('div#rcnt', 0);
    $images = $html->find('img');
    $image_count = 1; //Enter the amount of images to be shown
    $i = 0;
        foreach($images as $image){
            $srcimg = $image->src;
            if($i == $image_count) break;
            $i++;
           $randname = $imgname.".jpg";
         $randname = "Images/".$randname;
         file_put_contents("$randname", file_get_contents($srcimg));
        }
    }
}
?>

任何想法?

這對我有用。 simple_html_dom.php不會解決問題,因為“大圖”位於DOM中每個縮略圖附近的JSON代碼段內。

<?php
$search_query = "Some Keyword"; //change this
$search_query = urlencode( $search_query );
$googleRealURL = "https://www.google.com/search?hl=en&biw=1360&bih=652&tbs=isz%3Alt%2Cislt%3Asvga%2Citp%3Aphoto&tbm=isch&sa=1&q=".$search_query."&oq=".$search_query."&gs_l=psy-ab.12...0.0.0.10572.0.0.0.0.0.0.0.0..0.0....0...1..64.psy-ab..0.0.0.wFdNGGlUIRk";

// Call Google with CURL + User-Agent
$ch = curl_init($googleRealURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686; rv:20.0) Gecko/20121230 Firefox/20.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$google = curl_exec($ch);   
$array_imghtml = explode("\"ou\":\"", $google); //the big url is inside JSON snippet "ou":"big url"
foreach($array_imghtml as $key => $value){
  if ($key > 0) {
    $array_imghtml_2 = explode("\",\"", $value);
    $array_imgurl[] = $array_imghtml_2[0];
  }
}
var_dump($array_imgurl); //array contains the urls for the big images
die();
?>

我認為您可以使用Google自定義搜索API而不是抓取頁面

有關更多詳細信息,請參見以下網址: https//developers.google.com/custom-search/json-api/v1/overview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM