【问题标题】:Python: the right URL to download pictures from Google Image SearchPython:从 Google 图片搜索中下载图片的正确 URL
【发布时间】:2012-03-08 06:45:51
【问题描述】:

我正在尝试从 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)

我做错了什么?

谢谢

【问题讨论】:

  • 至少你要关闭文件句柄
  • @silviolor:我知道这对您的问题没有帮助,但为什么不使用 python 的内置 urllib2 模块而不是 curl

标签: python image scrape


【解决方案1】:

这是我用来从 Google 搜索和下载图片的 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

【讨论】:

  • 是否可以在给定的 url 中定义图像类型到谷歌
  • 我已经有一段时间没有看这个了,但是检查了最新的 Google API。我认为答案是肯定的,您可以将搜索细化为“.png”、“.jpg”,甚至是基于矢量的格式“.svg”。
【解决方案2】:

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

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

JULIE 和 NEWMAR 是您的搜索词。

这将返回您需要的 json 数据...您需要使用 json.loadsimplejson.load 解析该数据以获取 dict .. . 然后潜入其中,首先找到 responseData,然后是 results 列表,其中包含您想要下载其 url 的各个项目.

虽然我不建议以任何方式对 Google 进行自动抓取,因为他们的 (deprecated) API 为此明确表示不这样做。

【讨论】:

  • 请注意,此 API 不再可用。
【解决方案3】:

【讨论】:

  • 您好,您的脚本似乎正在使用 PIL。不幸的是,我在这台机器上安装 PIL 似乎遇到了很大的问题。由于我只需要图像,而不以任何方式转换它们,有没有办法摆脱它?
  • 我不确定如何避免 PIL,但如果您使用 Mac 来简化软件包安装并为您安装 PIL,我强烈推荐 MacPorts。
【解决方案4】:

我只是加入回答这个问题,即使它很旧。有一种更简单的方法可以做到这一点。

def google_image(x):
        search = x.split()
        search = '%20'.join(map(str, search))
        url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s&safe=off' % search
        search_results = urllib.request.urlopen(url)
        js = json.loads(search_results.read().decode())
        results = js['responseData']['results']
        for i in results: rest = i['unescapedUrl']
        return rest

就是这样。

【讨论】:

  • 这是在 3.x 中,所以在 2.x 中用 urllib2 替换 urllib.request 很明显。
【解决方案5】:

最好的方法之一是使用icrawler。检查下面的答案。它对我有用。

https://stackoverflow.com/a/51204611/4198099

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2013-11-07
    • 2012-12-08
    • 2014-01-10
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多