Last active
December 24, 2018 06:39
-
-
Save gologius/dffd310024fdc621ec78644a3362a3d1 to your computer and use it in GitHub Desktop.
適当な画像をDLします
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Sun Nov 18 17:24:58 2018 | |
@author: gologius | |
""" | |
from googleapiclient.discovery import build | |
import requests | |
import urllib | |
API_KEY = 'your api key' | |
SEARCH_ID = 'your id' | |
DOWNLOAD_PATH = r"\img" | |
# Wikipediaからランダムなキーワードを拾ってくる | |
WIKI_RANDOM_URL = 'https://ja.wikipedia.org/w/api.php?action=query&list=random&rnlimit=1&rnnamespace=0&format=json' | |
response = requests.get(WIKI_RANDOM_URL) | |
article = response.json()['query']['random'] | |
art_id = article[0]['id'] | |
art_title = article[0]['title'] | |
WIKI_ARTICLE_URL = 'https://ja.wikipedia.org/?curid=' + str(art_id) | |
print(art_title) | |
print(WIKI_ARTICLE_URL) | |
# Googleでそのキーワードでイメージ検索する ※大量の画像がほしいわけではないので、ページめくりは考慮しない | |
service = build("customsearch", "v1", developerKey=API_KEY) | |
responses = [] | |
try: | |
response = service.cse().list(q=art_title,cx=SEARCH_ID,num=5,start=1,searchType='image').execute() | |
responses.append(response) | |
except Exception as e: | |
print("gmail api error") | |
print(e) | |
#WEB画像情報のみ抽出 | |
img_urls = [] | |
for r in responses: | |
for i in r['items']: | |
img_urls.append(i['link']) | |
# 画像をダウンロードする | |
filepaths = [] | |
for url in img_urls: | |
try: | |
# ファイル名だけ取り出す | |
filename = url.split('/')[-1] | |
idx = filename.find("?") | |
if idx >= 0: | |
filename = filename[:idx] | |
filepath = DOWNLOAD_PATH + "\\" + art_title + "_"+ filename | |
urllib.request.urlretrieve(url, filepath) | |
filepaths.append(filepath) | |
print(filepath) | |
except Exception as e: | |
print("image download error") | |
print(e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment