python搜索图像谷歌图像

标签 python image search

我在使用 python 进行谷歌图片搜索时遇到了困难。我只需要使用标准 python 库(例如 urllib、urllib2、json、..)来完成它

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

我已经尝试了一百种不同的代码版本,使用 header 、用户代理、base64 编码、不同的 url (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=H6RaTtb5JcTeiALLlmPi2CQ&ved=0CDsQ9Q8等等....)

没有任何效果,它总是一个错误,404、401 或损坏的管道 :(

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

谢谢谁能解决这个问题,

戴夫:)

最佳答案

我在 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)

您还可以找到非常有用的信息here .

关于python搜索图像谷歌图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11242967/

相关文章:

python - 从大文件中打印行号之间的行

python - 如何在 python 中规范化 URL

javascript - 与图像查看器一起使用时放大镜 JavaScript 出现故障

image - 使用任意 QML 项作为缓存图像源

php - 不需要在 MySQL 中精确搜索

python - Pandas:迭代两个不同的列

python - 安卓工作室 : IncorrectOperationException when 'Add as Library' is clicked whilst trying to configure Google Apps Endpoints client libraries

c# - 如何使用 javascript 将 blob 图像从 html 页面上传到 C# 代码

jquery - 如何在 github 存储库的子文件夹中搜索特定单词或条目

algorithm - 自动获取术语列表,导入 Windows 搜索功能(针对内容),并导出结果列表。 (自动?)