###実現したいこと
Google APIを使って画像を自動収集したい。
そのため、いろいろ調べていたら、方法があったため、
いざ実行しようとしたら、エラーが出てしまった。
コード↓
lang
1import requests 2import urllib 3import shutil 4 5API_PATH = "**********************" 6 "cx" : "**************************", #検索エンジンID 7 "key": "**********************", #APIキー 8 "q" : "1", #検索ワード 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 27
エラー
Traceback (most recent call last): File "dataset.py", line 22, in <module> r = requests.get(item_json['link'], stream=True) TypeError: tuple indices must be integers or slices, not str
TypeErrorについていろいろと調べていても、
うまく実行できないためここで止まっています。
どなたか教えてください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/04 06:56
2018/07/04 07:00
2018/07/04 07:21