I cleaned it up a bit, let me know if you have questions or it doesn’t work. The output is a csv.
import urllib.request
import urllib.error
import json
import csv
import time
# see https://api.inaturalist.org/v1/docs/#!/Taxa/get_taxa for more details on parameters
# in particular, if there are more than 10,000 results, you'll need to pare it down via parameters to get everything
taxon = 45933 # specify the taxon number here
rank = 'species' # use '' (empty quotes) if you don't want to specify a rank
# by default calls only for active taxa, doesn't return all the names for each taxon, and 200 results per page
apiurl = 'https://api.inaturalist.org/v1/taxa?is_active=true&all_names=false&per_page=200'
def call_api(sofar=0, page=1):
"""Call the api repeatedly until all pages have been processed."""
try:
response = urllib.request.urlopen(apiurl + '&page=' + str(page) + '&taxon_id=' + str(taxon) + '&rank=' + rank)
except urllib.error.URLError as e:
print(e)
else:
responsejson = json.loads(response.read().decode())
for species in responsejson['results']:
# lots of possible data to keep, here it's name, taxon id, and observations count
csvwriter.writerow([species['name'], species['id'], species['observations_count']])
if (sofar + 200 < responsejson['total_results']): # keep calling the API until we've gotten all the results
time.sleep(1) # stay under the suggested API calls/min, not strictly necessary
call_api(sofar + 200, page + 1)
try:
with open(str(taxon) +'.csv', encoding='utf-8', mode='w+', newline='') as w: # open a csv named for the taxon
csvwriter = csv.writer(w, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
call_api()
except Exception as e:
print(e)