python搜索图像谷歌图像 [英] python search with image google images

查看:144
本文介绍了python搜索图像谷歌图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用python搜索谷歌图片搜索。我需要只使用标准的python库(所以urllib,urllib2,json,..)

i'm having a very tough time searching google image search with python. I need to do it using only standard python libraries (so urllib, urllib2, json, ..)

有人可以帮忙吗?假设图像是jpeg.jpg并且在我正在运行python的同一个文件夹中。

Can somebody please help? Assume the image is jpeg.jpg and is in same folder I'm running python from.

我尝试了一百个不同的代码版本,使用头文件,用户代理,base64编码,不同的网址(images.google.com, http://images.google.com/searchbyimage?hl=en&biw=1060&bih=766&gbv=2&site=search&image_url= {{URL to your image}} & sa = X& ei = H6RaTtb5JcTeiALlmPi2CQ& ved = 0CDsQ9Q8等....)

I've tried a hundred different code versions, using headers, user-agent, base64 encoding, different urls (images.google.com, http://images.google.com/searchbyimage?hl=en&biw=1060&bih=766&gbv=2&site=search&image_url={{URL To your image}}&sa=X&ei=H6RaTtb5JcTeiALlmPi2CQ&ved=0CDsQ9Q8, etc....)

没有任何作用,它始终是错误,404,401或管道损坏: (

Nothing works, it's always an error, 404, 401 or broken pipe :(

请给我看一些python脚本,它会用我自己的图像搜索谷歌图像作为搜索数据('jpeg.jpg'存储在我的计算机/设备上)

Please show me some python script that will actually seach google images with my own image as the search data ('jpeg.jpg' stored on my computer/device)

感谢你们任何人都可以解决这个问题,

Thank you for whomever can solve this,

戴夫:)

推荐答案

我在Python中使用以下代码来搜索Google图像并将图像下载到我的电脑:

I use the following code in Python to search for Google images and download the images to my computer:

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)

您还可以在此处找到非常有用的信息。

You can also find very useful information here.

这篇关于python搜索图像谷歌图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆