LoginSignup
13

More than 5 years have passed since last update.

犬画像をGoogle画像検索から保存

Last updated at Posted at 2015-08-14
  • !!このGoogle Image APIは使えなくなっています!!

参考

準備

sudo pip install httplib2

コード

get_imgs.py
#-*- coding:utf-8 -*-
import json
import os
import urllib2
import httplib2
import shutil

# search_wordの画像リンクを探索
def get_img_urls(search_word, n):
    img_url=[]
    images_per_request=8
    # https://developers.google.com/image-search/v1/jsondevguide
    url = "http://ajax.googleapis.com/ajax/services/search/images?q={0}&v=1.0&imgsz=huge&rsz="+str(images_per_request)+"&start={1}"
    urlencoded = urllib2.quote(search_word)
    # search_wordと一致する画像URLをn個取得
    for i in range(n):
        res = urllib2.urlopen(url.format(urlencoded, i*images_per_request))
        data = json.load(res)
        img_url += [result["url"] for result in data["responseData"]["results"]]
    return img_url

# URL先の画像ファイルを保存
def url_download(search_word,urls):
    if os.path.exists(search_word)==False:
        os.mkdir(search_word)
    opener = urllib2.build_opener()
    http = httplib2.Http(".cache")
    # URLの数だけ画像DL
    for i in range(len(set(urls))):
        try:
            fn, ext = os.path.splitext(urls[i])
            response, content = http.request(urls[i])
            with open(str(i)+ext, 'wb') as f:
                f.write(content)
            shutil.move((str(i)+ext).encode("utf-8"), search_word)
        except:
            continue

# nは10だと失敗した。8ならok
def main(search_word, n):
    urls = get_img_urls(search_word, n)
    url_download(search_word,urls)

if __name__ == '__main__':
    main("子犬 チワワ", 2)
    main("チワワ お座り", 2)
  • images_per_request=8 としているので、上記例では2*8枚取得します。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13