APIにGETリクエストを送り、レスポンスを100万行ごとにファイルに書き込むプログラムを書いたのですが、表記のエラーが出てしまい、解決できません。何か解決方法があれば教えていただきたいです。
<エラー本文>
StreamConsumedError Traceback (most recent call last)
<ipython-input-10-8637554a74a9> in <module>
3 decoded_lines = ''
4 with open(path.format(i), 'w') as f:
----> 5 for line in r.iter_lines():
6 count += 1
7 if count == i * 1000000:
~/opt/anaconda3/lib/python3.7/site-packages/requests/models.py in iter_lines(self, chunk_size, decode_unicode, delimiter)
792 pending = None
793
--> 794 for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
795
796 if pending is not None:
~/opt/anaconda3/lib/python3.7/site-packages/requests/models.py in iter_content(self, chunk_size, decode_unicode)
767
768 if self._content_consumed and isinstance(self._content, bool):
--> 769 raise StreamConsumedError()
770 elif chunk_size is not None and not isinstance(chunk_size, int):
771 raise TypeError("chunk_size must be an int, it is instead a %s." % type(chunk_size))
StreamConsumedError:
Python3
1base = 'https://api.nazuki-oto.com/historical/tweets?type=stream&estid={}' 2endpoint = 'kargrNyc' 3header = {'Accept-Encoding' : 'deflate, gzip'} 4url = base.format(endpoint) 5s = requests.Session() 6r = s.get(url, auth=('id', 'pass'), headers = header, stream=True) 7 8for i in range(1, 7): 9 count = 0 10 decoded_lines = '' 11 with open(path.format(i), 'w') as f: 12 for line in r.iter_lines(): 13 count += 1 14 if count == i * 1000000: 15 decoded_lines += line.decode('utf-8') 16 break 17 elif count > (i - 1) * 1000000: 18 decoded_lines += line.decode('utf-8') + '\n' 19 else: 20 pass 21 f.write(decoded_lines)
回答1件
あなたの回答
tips
プレビュー