Home>

I want to collect Pikachu images using the Google Custom Search API to collect machine learning training images. Please tell me what to do! !

import requests
import shutil
API_PATH = "https://www.googleapis.com/customsearch/v1"
PARAMS = {
  "cx": "", #Search engine ID
  "key": "", #API key
  "q": "Pikachu", #search word
  "searchType": "image", #search type
  "start": 1, #start index
  "num": 10 # of records retrieved per search (10 by default)
}
LOOP = 100
image_idx = 0
for x in range (LOOP):
  PARAMS.update ({'start': PARAMS ["num"] * x + 1})
  items_json = requests.get (API_PATH, PARAMS) .json () ["items"]
  for item_json in items_json:
    path = "images /" + str (image_idx) + ".png"
    r = requests.get (item_json ['link'], stream = True)
    if r.status_code == 200:
      with open (path, 'wb') as f:
        r.raw.decode_content = True
        shutil.copyfileobj (r.raw, f)
      image_idx + = 1 ###
Error message displayed

Traceback (most recent call last):
File"C: /Users/saito/PycharmProjects/kenkyu/kikaigakusyu.py" ;, line 23, in<module>
with open (path,'wb') as f:
FileNotFoundError: [Errno 2] No such file or directory:'images/0.png'