「NoneType' object is not subscriptable」このようなエラーが現れました。何行目がエラー起こしてますよ。などといった情報もなくこの一文のみの表示でした。どこがうまく処理されていないか、教えていただきたいです。
Python(JupyterNotebook)でGoogle Custom Search APIを使って画像収集を作っています。
■以下のコードを入力した際に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
NoneType' object is not subscriptable
該当のソースコード
import os import json import datetime from time import sleep from googleapiclient.discovery import build GOOGLE_API_KEY = "*********" CUSTOM_SEARCH_ENGINE_ID = "**********" DATA_DIR = 'ディレクトリ名' def makeDir(path): if not os.path.isdir(path): os.mkdir(path) today = datetime.datetime.today().strftime("%Y%m%d") timestamp = datetime.datetime.today().strftime("%Y/%m/%d %H:%M:%S") makeDir(DATA_DIR) service = build("customsearch", "v1", developerKey=GOOGLE_API_KEY) page_limit = 10 start_index = 1 response = [] for num_page in range(0, page_limit): try: sleep(1) response.append(service.cse().list( q='検索キーワード', cx=CUSTOM_SEARCH_ENGINE_ID, lr='lang_ja', num=10, start=start_index ).execute()) start_index = response[num_page].get("queries").get("nextPage")[0].get("startIndex") except Exception as e: print(e) break # レスポンスをjson形式で保存 save_response_dir = os.path.join(DATA_DIR, 'response') makeDir(save_response_dir) out = {'snapshot_ymd': today, 'snapshot_timestamp': timestamp, 'response': []} out['response'] = response jsonstr = json.dumps(out, ensure_ascii=False) with open(os.path.join(save_response_dir, 'response_' + today + '.json'), mode='w') as response_file: response_file.write(jsonstr)
回答1件
あなたの回答
tips
プレビュー