【问题标题】:python search with image google imagespython搜索图片谷歌图片
【发布时间】:2012-06-28 10:47:12
【问题描述】:

我很难用 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=search&image_url={{URL To your image}}&sa=X&ei=H6RaTtb5JcTeiALlmPi2CQ&ved=0CDsQ9Q8等……)

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

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

谢谢谁能解决这个问题,

戴夫:)

【问题讨论】:

  • Google 在阻止您抓取他们的网页方面比您在规避他们的保护方面做得更好,这可能并不令人惊讶。
  • 不,更多的是我不懂urllib2。无论我是通过浏览器搜索,还是通过我的 android 手机通过 python 搜索,有时我都可以毫无错误地发布,但得到的结果我只是不明白。我已经研究 urllib2 好几天了,它似乎无处不在,有 mimetypes、标头、几种 urllib.. 然后有改变的配方......但没有关于如何正确使用 urllib 或 urllib2 的手册.网上有很多帖子。。但每一个都不一样。例如,这是一个发布到谷歌翻译的帖子:
  • 这个 python 脚本可能会有所帮助:bit.ly/QjIy21

标签: python image search


【解决方案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)

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

【讨论】:

  • 数据有时可能是无。
  • 这是如何获得投票的?它根本没有回答OP的问题。问题是“请给我看一些 python 脚本,它实际上会搜索谷歌图像用我自己的图像作为搜索数据('jpeg.jpg' 存储在我的计算机/设备上)”。
  • 另外请注意,使用他们的 API 从谷歌搜索中保存图片直接违反了他们的条款和服务here
  • 不幸的是,该 API 现已弃用
【解决方案2】:

Google 图片搜索 API 已弃用,我们使用 google 搜索使用正则表达式和 Beautiful soup 下载图片

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()

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多