2011-02-12 40 views
3

我想獲取谷歌圖像對任何查詢。我已經通過谷歌圖片搜索api,但無法理解。我也看到了一些方法,他們提取圖像,但只有第一頁。我使用了以下方法。如何獲取谷歌圖片

 
function getGoogleImg($k) 
{ 
    $url = "http://images.google.it/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y"; 
    $web_page = file_get_contents(str_replace("##query##",urlencode($k), $url)); 
    $tieni = stristr($web_page,"dyn.setResults("); 
    $tieni = str_replace("dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni)); 
    $tieni = str_replace("[]","",$tieni); 
    $m = preg_split("/[\[\]]/",$tieni); 
    $x = array(); 
    for($i=0;$i<count($m);$i++) 
    { 
     $m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]); 
     $m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]); 
     $m[$i] = preg_replace("/^\"/i","",$m[$i]); 
     $m[$i] = preg_replace("/^,/i","",$m[$i]); 
     if ($m[$i]!="") 
     array_push($x,$m[$i]); 
    } 
    return $x; 
} 

該函數只返回21個圖像。我想要這個查詢的所有圖像。我正在做這個在PHP

+0

你見過一個谷歌-resultpage,給你所有(可以是數百萬)結果?不過,你最好使用圖像搜索API:http://code.google.com/intl/de/apis/imagesearch/ –

+0

是Dr.Mollie。但是當它返回時,它只返回其中的一部分。並非全部。因爲我們不能報廢谷歌圖片。 –

+0

請向我展示其中一個結果頁(包含超過20張圖片) –

回答

1

可悲的是圖像API正在關閉,所以我不會建議移動到那,但這將是一個更好的解決方案,我認爲。

我最好的猜測是圖像22和轉發正在使用som ajax/javascript進行某種加載(如果您搜索標識並向下滾動,您會看到佔位符會在您向下移動時加載),而且您需要通過JavaScript引擎傳遞頁面,這不是我可以找到任何人已經完成的PHP(但)。 你有沒有檢查$ web_page包含超過21個圖像(當我玩Google圖像搜索時,它使用JavaScript來加載一些圖像)? 當您從普通瀏覽器訪問鏈接時,會發生什麼情況,如果關閉JavaScript會發生什麼情況? 您有結果中是否有鏈接到下一頁?

在現在反對的圖像API有辦法限制每個頁面的結果和方法步驟到下一個頁面https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php

的數量。如果你想繼續從搜索結果做搜索和獲取圖像,然後對於以後的http://simplehtmldom.sourceforge.net/可能是一個不錯的選擇。 它提取html DOM,並允許您輕鬆找到節點並使其易於使用。但它仍然使用file_get_contents或curl庫來獲取數據,所以它可能需要一些小工具才能使JavaScript工作。

1

我寫了一個腳本下載圖像形成我正在下載100個的原始圖像

原來的劇本我對計算器的答案寫谷歌圖片搜索

Python - Download Images from google Image search?

,我將詳細介紹如何解釋我使用的urllib2和BeautifulSoup

例如刮原始圖像的URL從谷歌圖片搜索如果u想湊電影終結者3來來往往的圖像米穀歌圖片搜索

query= "Terminator 3" 
query= '+'.join(query.split()) #this will make the query terminator+3 
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch" 
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36" 
} 
req = urllib2.Request(url,headers=header) 
soup= urllib2.urlopen(req) 
soup = BeautifulSoup(soup) 
上述

變量湯所含現在要求我們需要提取的圖像u必須在瀏覽器中打開網頁,並做了檢查元素的網頁的HTML代碼圖像

在這裏你會找到包含URL

的圖像,例如用於谷歌圖片,我發現「格」,{「級」:「rg_meta」}標籤包含的鏈接圖像

你可以搜索BeautifulSoup doccummen塔季翁

print soup.find_all("div",{"class":"rg_meta"}) 

U將得到的結果列表作爲

<div class="rg_meta">{"cl":3,"cr":3,"ct":12,"id":"C0s-rtOZqcJOvM:","isu":"emuparadise.me","itg":false,"ity":"jpg","oh":540,"ou":"http://199.101.98.242/media/images/66433-Terminator_3_The_Redemption-1.jpg","ow":960,"pt":"Terminator 3 The Redemption ISO \\u0026lt; GCN ISOs | Emuparadise","rid":"VJSwsesuO1s1UM","ru":"http://www.emuparadise.me/Nintendo_Gamecube_ISOs/Terminator_3_The_Redemption/66433","s":"Screenshot Thumbnail/Media File 1 for Terminator 3 The Redemption","th":168,"tu":"https://encrypted-tbn2.gstatic.com/images?q\\u003dtbn:ANd9GcRs8dp-ojc4BmP1PONsXlvscfIl58k9hpu6aWlGV_WwJ33A26jaIw","tw":300}</div> 

上面的結果中包含鏈接到我們的圖片URL

http://199.101.98.242/media/images/66433-Terminator_3_The_Redemption-1.jpg 

U可以如下

提取這些鏈接和圖像
ActualImages=[]# contains the link for Large original images, type of image 
for a in soup.find_all("div",{"class":"rg_meta"}): 
    link , Type =json.loads(a.text)["ou"] ,json.loads(a.text)["ity"] 
    ActualImages.append((link,Type)) 

for i , (img , Type) in enumerate(ActualImages): 
    try: 
     req = urllib2.Request(img, headers={'User-Agent' : header}) 
     raw_img = urllib2.urlopen(req).read() 
     if not os.path.exists(DIR): 
      os.mkdir(DIR) 
     cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1 
     print cntr 
     if len(Type)==0: 
      f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb') 
     else : 
      f = open(DIR + image_type + "_"+ str(cntr)+"."+Type, 'wb') 


     f.write(raw_img) 
     f.close() 
    except Exception as e: 
     print "could not load : "+img 
     print e 

瞧現在ü可以使用這個腳本從谷歌搜索

或採集訓練圖像

爲全工作腳本下載圖像,你可以在這裏

https://gist.github.com/rishabhsixfeet/8ff479de9d19549d5c2d8bfc14af9b88