파이썬에서 Google Custom Search API를 사용하여 이미지를 수집했습니다.
맞춤 검색 및 API 설정
우선 Custom Search와 API 설정을 합니다만, 아래의 URL을 참고해 보세요.
 맞춤 검색 및 API 설정
 이미지 수집 구현
images_get.pyimport urllib.request
from urllib.parse import quote
import httplib2
import json 
import os
import sys
import requests
API_KEY = "" # 取得したらここに追加
CUSTOM_SEARCH_ENGINE = "" # 取得したらここに追加
# キーワードを指定してください
keywords=["犬","猫"]
# 画像を検索し、URLをimg_urlsに追加
def get_image_url(search_keywords, total_num):
    img_urls = []
    i = 0
    while i < total_num:
        query_img = "https://www.googleapis.com/customsearch/v1?key=" + API_KEY + "&cx=" + CUSTOM_SEARCH_ENGINE + "&num=" + str(10 if(total_num-i)>10 else (total_num-i)) + "&start=" + str(i+1) + "&q=" + quote(search_keywords) + "&searchType=image"
        res = urllib.request.urlopen(query_img)
        data = json.loads(res.read().decode('utf-8'))
        for j in range(len(data["items"])):
            img_urls.append(data["items"][j]["link"])
        i += 10
    return img_urls
# 画像のURLから画像を保存
def get_image(search_keywords, img_urls,j):
    for i in range(len(img_urls)):
        res = requests.get(img_urls[i])
        get_images = res.content
        filename = search_keywords + str(i) + ".jpg"
        with open(filename, 'wb') as f:
            f.write(get_images)
# 実行
for j in range(len(keywords)):
    print(keywords[j])
    img_urls = get_image_url(keywords[j],5) # キーワードごとに取得したい枚数を指定(今回は5)
    get_image(keywords[j], img_urls,j)
이를 이미지를 넣으려는 디렉토리에서 수행하십시오.
이 방법으로 이미지를 얻을 수 있습니다.
 
이미지를 수집하고 싶을 때 활용하십시오.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(파이썬에서 Google Custom Search API를 사용하여 이미지를 수집했습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/kagami-r0927/items/f88d04cbb4a4f43a2c23
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
images_get.py
import urllib.request
from urllib.parse import quote
import httplib2
import json 
import os
import sys
import requests
API_KEY = "" # 取得したらここに追加
CUSTOM_SEARCH_ENGINE = "" # 取得したらここに追加
# キーワードを指定してください
keywords=["犬","猫"]
# 画像を検索し、URLをimg_urlsに追加
def get_image_url(search_keywords, total_num):
    img_urls = []
    i = 0
    while i < total_num:
        query_img = "https://www.googleapis.com/customsearch/v1?key=" + API_KEY + "&cx=" + CUSTOM_SEARCH_ENGINE + "&num=" + str(10 if(total_num-i)>10 else (total_num-i)) + "&start=" + str(i+1) + "&q=" + quote(search_keywords) + "&searchType=image"
        res = urllib.request.urlopen(query_img)
        data = json.loads(res.read().decode('utf-8'))
        for j in range(len(data["items"])):
            img_urls.append(data["items"][j]["link"])
        i += 10
    return img_urls
# 画像のURLから画像を保存
def get_image(search_keywords, img_urls,j):
    for i in range(len(img_urls)):
        res = requests.get(img_urls[i])
        get_images = res.content
        filename = search_keywords + str(i) + ".jpg"
        with open(filename, 'wb') as f:
            f.write(get_images)
# 実行
for j in range(len(keywords)):
    print(keywords[j])
    img_urls = get_image_url(keywords[j],5) # キーワードごとに取得したい枚数を指定(今回は5)
    get_image(keywords[j], img_urls,j)
이를 이미지를 넣으려는 디렉토리에서 수행하십시오.
이 방법으로 이미지를 얻을 수 있습니다.

이미지를 수집하고 싶을 때 활용하십시오.
Reference
이 문제에 관하여(파이썬에서 Google Custom Search API를 사용하여 이미지를 수집했습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kagami-r0927/items/f88d04cbb4a4f43a2c23텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)