8

私はPythonでグーグル画像検索を検索するのに非常に苦労しています。標準のPythonライブラリ(つまり、urllib、urllib2、json、..)のみを使用してそれを行う必要があります

誰か助けてもらえますか?画像がjpeg.jpgであり、Pythonを実行しているのと同じフォルダーにあると仮定します。

ヘッダー、ユーザーエージェント、base64エンコーディング、さまざまなURL(images.google.com、 http: //images.google.com/searchbyimage? hl = en&biw = 1060&bih = 766&gbv = 2&site)を使用して、100の異なるコードバージョンを試しました。= search&image_url = {{URL To your image}}&sa = X&ei = H6RaTtb5JcTeiALlmPi2CQ&ved = 0CDsQ9Q8など)

何も機能しません、それは常にエラー、404、401または壊れたパイプです:(

私自身の画像を検索データとして実際にグーグル画像を検索するPythonスクリプトをいくつか見せてください(私のコンピューター/デバイスに保存されている「jpeg.jpg」)

これを解決できる人に感謝します、

デイブ:)

4

2 に答える 2

1

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

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)

また、非常に役立つ情報もここで見つけることができます。

于 2012-11-24T07:31:10.510 に答える