Part1 Make function and class

  • Get Animal Species/Habitats/Threats/Conservation information from IUCN by scientific name

In [1]:
import requests
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize

In [43]:
def getIUCNbyName(api,sciName):
    token = '8a5016b7dba22be695228e3fb36f432779b64186e2385798ae9469ad2afd7443'
    url='http://apiv3.iucnredlist.org/api/v3/{}{}?token={}'.format(api,sciName,token)
    try:
        resp = requests.get(url)
        data = json.loads(resp.text)
        data_norm = json_normalize(data['result'])
        return data_norm
    except:
        return 'NE'
class Animal(object):
    def __init__(self,sciName):
        self.sciName = sciName
    def Species(self):
        return getIUCNbyName('species/',self.sciName)
    def Habitats(self):
        return getIUCNbyName('habitats/species/name/',self.sciName)
    def Threats(self):
        return getIUCNbyName('threats/species/name/',self.sciName)
    def Conservation(self):
        return getIUCNbyName('measures/species/name/',self.sciName)

Part2 Test


In [35]:
a = Animal('Mauremys reevesii')

In [37]:
print(a.Habitats()['code'])


0     15.1
1     15.2
2     15.3
3     15.7
4     15.8
5     15.9
6      5.4
7      5.6
8      5.7
9      5.8
10     5.9
Name: code, dtype: object

In [46]:
Animal('test test').Species()


Out[46]:
'NE'

In [8]:
CN_SpeciesList=getIUCNbyName('country/getspecies/','CN')
CN_SpeciesList.to_csv('CN_SpeciesList.csv')


http://apiv3.iucnredlist.org/api/v3/country/getspecies/CN?token=8a5016b7dba22be695228e3fb36f432779b64186e2385798ae9469ad2afd7443

In [55]:
sp_list = ['TEST','Abies chensiensis','Mauremys reevesii','Andrias davidianus','Nomascus leucogenys'] #秦岭冷杉/草龟/大鲵

In [57]:
for li in sp_list:
    print(Animal(li).Habitats())


NE
  code             habitat majorimportance season suitability
0  1.4  Forest - Temperate             Yes   None    Suitable
    code                                            habitat majorimportance  \
0   15.1  Artificial/Aquatic - Water Storage Areas (over...            None   
1   15.2             Artificial/Aquatic - Ponds (below 8ha)            None   
2   15.3             Artificial/Aquatic - Aquaculture Ponds            None   
3   15.7  Artificial/Aquatic - Irrigated Land (includes ...            None   
4   15.8  Artificial/Aquatic - Seasonally Flooded Agricu...            None   
5   15.9  Artificial/Aquatic - Canals and Drainage Chann...            None   
6    5.4  Wetlands (inland) - Bogs, Marshes, Swamps, Fen...            None   
7    5.6  Wetlands (inland) - Seasonal/Intermittent Fres...            None   
8    5.7  Wetlands (inland) - Permanent Freshwater Marsh...            None   
9    5.8  Wetlands (inland) - Seasonal/Intermittent Fres...            None   
10   5.9   Wetlands (inland) - Freshwater Springs and Oases            None   

   season suitability  
0    None    Marginal  
1    None    Marginal  
2    None    Marginal  
3    None    Marginal  
4    None    Marginal  
5    None    Marginal  
6    None    Suitable  
7    None    Suitable  
8    None    Suitable  
9    None    Suitable  
10   None    Suitable  
  code                                            habitat majorimportance  \
0  1.4                                 Forest - Temperate            None   
1  1.6        Forest - Subtropical/Tropical Moist Lowland            None   
2  1.9        Forest - Subtropical/Tropical Moist Montane            None   
3  5.1  Wetlands (inland) - Permanent Rivers/Streams/C...            None   

  season suitability  
0   None    Suitable  
1   None    Suitable  
2   None    Suitable  
3   None    Suitable  
  code                                      habitat majorimportance season  \
0  1.6  Forest - Subtropical/Tropical Moist Lowland             Yes   None   
1  1.9  Forest - Subtropical/Tropical Moist Montane             Yes   None   

  suitability  
0    Suitable  
1    Suitable  

In [7]:
sp_list_pingguo = ['Parabramis pekinensis',
'Ctenopharyngodon idellus',
'Manis pentadactyla',
'Paguma larvata',
'Gekko gecko',
'Marmota bobak',
'Trachypithecus francoisi',
'Centropus sinensis',
'Oecophylla smaragdina',
'Monopterus albus',
'Carassius auratus',
'Ptychidio jordani',
'Cyprinus carpio',
'Hypophthalmichthys molitrix',
'Moschus berezovskii',
'Calotes versicolor',
'Python molurus',
'Bubo zeylonensis',
'Semilabeo notabilis',
'Spinibarbus denticulatus',
'Palea steindachneri',
'Sus scrofa',
'Clarias fuscus',
'Rana rugulosa',
'Cygnus columbianus',
'Cuon alpinus',
'Phasianus colchicus',
'Aristichthys nobilis ',
'Francolinus pintadeanus']