Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用谷歌图片进行python搜索_Python_Image_Search - Fatal编程技术网

用谷歌图片进行python搜索

用谷歌图片进行python搜索,python,image,search,Python,Image,Search,我在用python搜索google图像搜索时遇到了一个非常困难的问题。我只需要使用标准的python库(比如urllib、urllib2、json等等)就可以了 有人能帮忙吗?假设图像是jpeg.jpg,并且位于运行python的同一文件夹中 我已经尝试了一百种不同的代码版本,使用了头、用户代理、base64编码、不同的URL(images.google.com、{{URL To your image}}&sa=X&ei=h6rattb5jctailmpi2cq&ved=0CDsQ9Q8等等)

我在用python搜索google图像搜索时遇到了一个非常困难的问题。我只需要使用标准的python库(比如urllib、urllib2、json等等)就可以了

有人能帮忙吗?假设图像是jpeg.jpg,并且位于运行python的同一文件夹中

我已经尝试了一百种不同的代码版本,使用了头、用户代理、base64编码、不同的URL(images.google.com、{{URL To your image}}&sa=X&ei=h6rattb5jctailmpi2cq&ved=0CDsQ9Q8等等)

什么都不起作用,总是出错,404、401或管道破裂:(

请给我看一些python脚本,这些脚本将实际搜索google图像,并将我自己的图像作为搜索数据(“jpeg.jpg”存储在我的计算机/设备上)

谢谢你能解决这个问题的人


Dave:)

我使用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)

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

谷歌图像搜索API已被弃用,我们使用谷歌搜索使用正则表达式和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()

谷歌在阻止你删除他们的页面方面比你在规避他们的保护方面做得更好,这可能并不奇怪。不,更重要的是我不理解urllib2。无论我是通过浏览器搜索,还是通过android手机通过python搜索,我有时都可以毫无错误地发布,但得到的结果我就是不明白。我已经研究urllib2好几天了,它似乎到处都是,有MIMEType,header,几种urllib。。然后是改变配方的。。。但是没有关于如何正确使用urllib或urllib2的手册。网上有很多帖子。。但每一个都是不同的。例如,这里有一个发布到google translate的帖子:这个python脚本可能会有帮助:有时数据可能是无的。这是如何得到提升的?它根本没有回答OP的问题。问题是“请给我看一些python脚本,这些脚本将实际搜索谷歌图像,并使用我自己的图像作为搜索数据('jpeg.jpg'存储在我的计算机/设备上)”。同样值得注意的是,使用他们的API保存谷歌搜索中的图像直接违反了他们的条款和服务,不幸的是,API现在已被弃用