2012-06-28 9 views
8

私はpythonでGoogle画像検索を検索するのが非常に苦労しています。標準のPythonライブラリ(urllib、urllib2、json、..)だけを使ってやる必要がありますpython search with image google images

誰か助けてもらえますか?イメージがjpeg.jpgで、同じフォルダにあるとします。私はPythonを実行しています。

I百異なるヘッダを使用してコードのバージョン、ユーザーエージェント、base64エンコーディング、異なるURL(images.google.com、 http://images.google.com/searchbyimage?hl=en&biw=1060&bih=766&gbv=2&site=search&image_url= {イメージを{URL}} & SA = X & EI = H6RaTtb5JcTeiALlmPi2CQを試みた

& VED = 0CDsQ9Q8、など...)

何も働かない、それは、常に404、401または壊れたパイプエラーです:(

私に実際に私自身でGoogle画像検索をWiki検索しますいくつかのPythonスクリプトをご提示ください画像を検索データとして使用します(コンピュータ/デバイスに保存された「jpeg.jpg」)

デイブ、この問題を解決することができます誰いただきありがとうございます:)

+0

を使用して画像をダウンロードするためにGoogle検索を使用するには、Googleがあなたよりも自分のページをこするからあなたを止めるに優れていることを、おそらくすべてのことは驚くべきことではないのです彼らの保護を迂回している。 – geoffspear

+0

いいえ、私はurllib2を理解していないだけです。ブラウザで検索しても、Androidの携帯電話でPythonで検索しても、時には間違いなく投稿することができますが、結果を得るのはわかりません。私は今のところurllib2を勉強してきましたが、それはちょうど全部のように見えますが、MIMEタイプ、ヘッダー、urllibのいくつかの品種があります..その後、レシピが変更されています... urllib、またはurllib2 。オンラインで多くの投稿がありますが、それぞれが異なっています。たとえば、ここにgoogleに投稿するものがあります。 – user1488252

+0

https://bitbucket.org/vgavro/google_translate/src/19807740244a/google_translate.py – user1488252

答えて

2

私は、Googleの画像を検索し、自分のコンピュータに画像をダウンロードするにはPythonで次のコードを使用します。

import os 
import sys 
import time 
from urllib import FancyURLopener 
import urllib2 
import simplejson 

# Define search term 
searchTerm = "hello world" 

# Replace spaces ' ' in search term for '%20' in order to comply with request 
searchTerm = searchTerm.replace(' ','%20') 


# Start FancyURLopener with defined version 
class MyOpener(FancyURLopener): 
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11' 
myopener = MyOpener() 

# Set count to 0 
count= 0 

for i in range(0,10): 
    # Notice that the start changes for each iteration in order to request a new set of images for each loop 
    url = ('https://ajax.googleapis.com/ajax/services/search/images?' + 'v=1.0&q='+searchTerm+'&start='+str(i*4)+'&userip=MyIP') 
    print url 
    request = urllib2.Request(url, None, {'Referer': 'testing'}) 
    response = urllib2.urlopen(request) 

    # Get results using JSON 
    results = simplejson.load(response) 
    data = results['responseData'] 
    dataInfo = data['results'] 

    # Iterate for each result and get unescaped url 
    for myUrl in dataInfo: 
     count = count + 1 
     print myUrl['unescapedUrl'] 

     myopener.retrieve(myUrl['unescapedUrl'],str(count)+'.jpg') 

    # Sleep for one second to prevent IP blocking from Google 
    time.sleep(1) 

することができますまた、非常に有用な情報を見つけるhere

+1

データがあります。 – itsuper7

+8

これはどのようにしてupvotedになったのですか? OPの質問には全く答えません。問題は「私のコンピュータ/デバイスに保存されているjpeg.jpg」という検索データとして私自身のイメージを使って**実際にGoogle画像を見る**というpythonスクリプトを表示してください。 – Natsukane

+2

また、APIを使用してGoogle検索から画像を保存することは、メモと同様に、[ここ](https://developers.google.com/image-search/terms)に記載されている利用規約に直接違反します。 –

1

Googleの画像検索APIは廃止され、我々は正規表現と美しいスープ

from bs4 import BeautifulSoup 
import requests 
import re 
import urllib2 
import os 


def get_soup(url,header): 
    return BeautifulSoup(urllib2.urlopen(urllib2.Request(url,headers=header))) 

image_type = "Action" 
# you can change the query for the image here 
query = "Terminator 3 Movie" 
query= query.split() 
query='+'.join(query) 
url="https://www.google.co.in/searches_sm=122&source=lnms&tbm=isch&sa=X&ei=4r_cVID3NYayoQTb4ICQBA&ved=0CAgQ_AUoAQ&biw=1242&bih=619&q="+query 

print url 
header = {'User-Agent': 'Mozilla/5.0'} 
soup = get_soup(url,header) 

images = [a['src'] for a in soup.find_all("img", {"src": re.compile("gstatic.com")})] 
#print images 
for img in images: 
    raw_img = urllib2.urlopen(img).read() 
    #add the directory for your image here 
    DIR="C:\Users\hp\Pictures\\valentines\\" 
    cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1 
    print cntr 
    f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb') 
    f.write(raw_img) 
    f.close()