2012-02-16 105 views
6

我在尝试从Google图片搜索中获取特定查询的图片。但我下载的页面没有图片,它将我重定向到Google的原始页面。这是我的代码:Python:从Google图片搜索下载图片的正确网址

AGENT_ID = "Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1" 

GOOGLE_URL = "https://www.google.com/images?source=hp&q={0}" 

_myGooglePage = "" 

def scrape(self, theQuery) : 
    self._myGooglePage = subprocess.check_output(["curl", "-L", "-A", self.AGENT_ID, self.GOOGLE_URL.format(urllib.quote(theQuery))], stderr=subprocess.STDOUT) 
    print self.GOOGLE_URL.format(urllib.quote(theQuery)) 
    print self._myGooglePage 
    f = open('./../../googleimages.html', 'w') 
    f.write(self._myGooglePage) 

我在做什么错?

感谢

+1

至少你必须关闭文件句柄 – 2012-02-16 20:38:21

+0

它的工作!谢谢 – lorussian 2012-02-16 20:43:26

+0

@silviolor:我知道它不会帮助你的问题,但为什么不使用Python的内置'urllib2'模块而不是'curl'。 – RanRag 2012-02-16 21:14:45

回答

3

我会给你一个提示......从这里开始:

https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=JULIE%20NEWMAR

凡朱莉和莉纽玛是你的搜索条件。

,将返回您需要的JSON数据......你需要解析使用json.loadsimplejson.load找回的字典......随后出现跳水进去先找到responseData,然后结果列表,其中包含您将要下载的各个项目的其中的url

虽然我不建议以任何方式进行Google的自动抓取,因为它们的(deprecated) API因此专门说不适用。

+0

谢谢,这种方式看起来更容易。 – lorussian 2012-02-17 00:37:10

+0

请注意,此API不再可用。 – prooffreader 2016-02-29 17:45:39

3
+0

你好,你的脚本似乎在使用PIL。不幸的是,我在这台机器上安装PIL似乎有巨大的问题。既然我只是需要图像,而不以任何方式改变它们,有没有办法让它脱身呢? – 2012-07-08 10:18:36

+0

我不确定如何避免PIL,但如果您使用Mac来简化软件包安装并为您安装PIL,我强烈建议使用MacPorts。 – crizCraig 2012-07-09 20:07:06

+0

或更好,自制软件:http://brew.sh/ – 2013-09-01 16:27:37

6

这是在Python代码,我用它来搜索和谷歌从下载图像,希望它有助于:

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

+0

是否可以在给定的网址上定义图片类型为Google – erogol 2014-08-09 09:11:47

+0

我暂时没有看这个,但查看最新的Google API。我认为答案是肯定的,您可以将搜索结果细化为“.png”,“.jpg”,甚至是基于矢量的格式“.svg”。 – 2014-08-09 17:41:29

0

我只是在回答这个问题,尽管它很古老。有一个更简单的方法去做这件事。

​​

就是这样。

+0

这是3.x,所以用2.x中的urllib2替换urllib.request显然。 – riyoken 2013-09-11 19:28:12