I'm retrieving data from an API, but can not get it to save to a file.
import requests
import json
response = requests.get(f'url', headers={'CERT': 'cert'})
response = response.json()
When I run response
or respons.json()
I can see the data that I am wanting to record.
I have tried:
str1 = ''.join(map(str, response))
with open('data.txt', mode ='a+') as f:
f.write(f'{str1}')
print("File appended successfully")
f.close()
str1 = ''.join(map(str, response))
with open('data.json', mode ='a+') as f:
f.write(f'{str1}')
print("File appended successfully")
f.close()
with open('data.json', mode ='a+') as results:
result = json.loads(results)
results.write(json.dumps(result, indent=4))
with requests.get(f'url', headers={'CERT': 'cert'}, stream=True) as r:
r.raise_for_status()
with open('data.json', 'wb') as f_out:
for chunk in r.iter_content(chunk_size=8192):
f_out.write(chunk)
with open('data.txt', mode ='a+') as f:
for items in response:
f.write('%s\n' % items)
print("File appended successfully")
f.close()
with open('data.json', mode ='a+') as f:
for items in response:
f.write('%s\n' % items)
print("File appended successfully")
f.close()
And a few other variations with no luck. Can someone point me in the right direction or let me know what I'm doing incorrectly to get the data from the response
variable to actually populate in the file?