機械学習の訓練画像を収集するためにGoogle Custom Search APIを用いてピカチュウの画像を集めたいのですがうまくいきません。どうすれば良いか教えてください!!
python
1import requests 2import shutil 3 4API_PATH = "https://www.googleapis.com/customsearch/v1" 5PARAMS = { 6 "cx" : " ", #検索エンジンID 7 "key": " ", #APIキー 8 "q" : "ピカチュウ", #検索ワード 9 "searchType": "image", #検索タイプ 10 "start" : 1, #開始インデックス 11 "num" : 10 #1回の検索における取得件数(デフォルトで10件) 12} 13LOOP = 100 14image_idx = 0 15 16for x in range(LOOP): 17 PARAMS.update({'start': PARAMS["num"] * x + 1}) 18 items_json = requests.get(API_PATH, PARAMS).json()["items"] 19 for item_json in items_json: 20 path = "images/" + str(image_idx) + ".png" 21 r = requests.get(item_json['link'], stream=True) 22 if r.status_code == 200: 23 with open(path, 'wb') as f: 24 r.raw.decode_content = True 25 shutil.copyfileobj(r.raw, f) 26 image_idx+=1###
###表示されているエラーメッセージ
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'
APIキーはパスワードのようなものなので、掲載しないほうがよいかと思います。 "Publicly exposing your credentials can result in your account being compromised, which could lead to unexpected charges on your account." https://support.google.com/googleapi/answer/6310037
質問の編集でGoogle APIのタグを削除されたようですが、問題なのはソースコードにAPIキーが含まれていることです。もし意味が分からなければ、Googleアカウントのハッキング防止のためこの質問自体を削除することをおすすめします。
回答1件
あなたの回答
tips
プレビュー