You can make a string out of your JSON like this:
import json
json_str = json.dumps(r.json())
And then save it as a regular text file using standard methods.
with open(filename, 'w') as f:
f.write(json_str)
EDIT: as pointed out in comments by @Tomalak, this code does double conversion, which is clearly not needed here. So you can just
with open(filename, 'w') as f:
f.write(r.text)
Answer from Pavel Gurkov on Stack OverflowIt would be the files listed here. https://open.fda.gov/apis/drug/event/download/
I have been using youtube videos on how to bulk download but I haven't figure it out.
Videos
Ok so I had a lot of trouble interacting with the site. I decided to just go with the webbrowser library.
import webbrowser
chrome_path="C:xxx\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path))
url = 'http://testsite/csv?date=2019-07-18'
Setting chrome to download files automatically populates my download folder from where i can automate everything else :)
You need to read the data out of the object that urlopen returns.
Try
import urllib
with urllib.request.urlopen("test.com/csv?date=2019-07-17") as f:
jsonl = f.read()
» pip install jsons
» pip install jsonlib
Get data from the URL and then call json.loads e.g.
Python3 example:
import urllib.request, json
with urllib.request.urlopen("http://maps.googleapis.com/maps/api/geocode/json?address=google") as url:
data = json.loads(url.read().decode())
print(data)
Python2 example:
import urllib, json
url = "http://maps.googleapis.com/maps/api/geocode/json?address=google"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data
The output would result in something like this:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Charleston and Huff",
"short_name" : "Charleston and Huff",
"types" : [ "establishment", "point_of_interest" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
...
I'll take a guess that you actually want to get data from the URL:
jsonurl = urlopen(url)
text = json.loads(jsonurl.read()) # <-- read from it
Or, check out JSON decoder in the requests library.
import requests
r = requests.get('someurl')
print r.json() # if response type was set to JSON, then you'll automatically have a JSON response here...