Modules, Requests und arbeiten mit APIs

 0. Importiere die Module requests und pandas, die verwenden würdest


In [10]:
!pip install requests


Requirement already satisfied: requests in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)
Requirement already satisfied: idna<2.7,>=2.5 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)
Requirement already satisfied: certifi>=2017.4.17 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)

In [2]:
!pip install requests


Requirement already satisfied: requests in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages
Requirement already satisfied: certifi>=2017.4.17 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)
Requirement already satisfied: idna<2.7,>=2.5 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /Users/svenmillischer/.virtualenvs/python3/lib/python3.6/site-packages (from requests)

In [11]:
import requests

In [12]:
import pandas as pd

1. Suche ein paar Beispiele von APIs. Von solchen, die mit und ohne Schlüssel, verwendet werden können und liste sie auf.


In [ ]:
https://developers.google.com/maps/documentation/javascript/ #google maps braucht einen schlüssel
https://wiki.musicbrainz.org/Development/JSON_Web_Service #musicbrainz braucht keinen key
https://developer.spotify.com/web-api/ #spotify braucht keinen key
https://docs.openaq.org/ #luftqualität brauchtt keinen key

 2. Lese eine dieser APIs mit requests ein und rufe die Ergebnisse auf.


In [13]:
url = "https://api.openaq.org/v1/cities"

In [14]:
response = requests.get(url)

In [15]:
response.json()


Out[15]:
{'meta': {'found': 1881,
  'license': 'CC BY 4.0',
  'limit': 100,
  'name': 'openaq-api',
  'page': 1,
  'website': 'https://docs.openaq.org/'},
 'results': [{'city': 'Escaldes-Engordany',
   'count': 2377,
   'country': 'AD',
   'locations': 2},
  {'city': 'unused', 'count': 290, 'country': 'AD', 'locations': 1},
  {'city': 'Buenos Aires', 'count': 15840, 'country': 'AR', 'locations': 4},
  {'city': 'Gemeinde Wien, MA22 Umweltschutz',
   'count': 136958,
   'country': 'AT',
   'locations': 21},
  {'city': 'Amt der Nieder�sterreichischen Landesregierung',
   'count': 339068,
   'country': 'AT',
   'locations': 39},
  {'city': 'Austria', 'count': 121987, 'country': 'AT', 'locations': 174},
  {'city': 'Amt der Steierm�rkischen Landesregierung',
   'count': 336912,
   'country': 'AT',
   'locations': 41},
  {'city': 'Amt der K�rntner Landesregierung',
   'count': 109995,
   'country': 'AT',
   'locations': 16},
  {'city': 'Amt der Ober�sterreichischen Landesregierung',
   'count': 162343,
   'country': 'AT',
   'locations': 16},
  {'city': 'Umweltinstitut des Landes Vorarlberg',
   'count': 37996,
   'country': 'AT',
   'locations': 9},
  {'city': 'Amt der Tiroler Landesregierung',
   'count': 119040,
   'country': 'AT',
   'locations': 19},
  {'city': 'Amt der Burgenl�ndischen Landesregierung',
   'count': 37454,
   'country': 'AT',
   'locations': 3},
  {'city': 'Amt der Burgenländischen Landesregierung',
   'count': 471,
   'country': 'AT',
   'locations': 1},
  {'city': 'Amt der Oberösterreichischen Landesregierung',
   'count': 2828,
   'country': 'AT',
   'locations': 6},
  {'city': 'Umweltbundesamt', 'count': 85183, 'country': 'AT', 'locations': 8},
  {'city': 'Amt der Steiermärkischen Landesregierung',
   'count': 1891,
   'country': 'AT',
   'locations': 4},
  {'city': 'Amt der Salzburger Landesregierung',
   'count': 99008,
   'country': 'AT',
   'locations': 12},
  {'city': 'Amt der Kärntner Landesregierung',
   'count': 471,
   'country': 'AT',
   'locations': 1},
  {'city': 'Amt der Niederösterreichischen Landesregierung',
   'count': 1413,
   'country': 'AT',
   'locations': 3},
  {'city': 'Illawarra', 'count': 232413, 'country': 'AU', 'locations': 3},
  {'city': 'South-west Slopes',
   'count': 52619,
   'country': 'AU',
   'locations': 2},
  {'city': 'Gladstone', 'count': 155224, 'country': 'AU', 'locations': 8},
  {'city': 'Sydney South-west',
   'count': 402134,
   'country': 'AU',
   'locations': 6},
  {'city': 'Central Tablelands',
   'count': 26874,
   'country': 'AU',
   'locations': 1},
  {'city': 'Lower Hunter', 'count': 232147, 'country': 'AU', 'locations': 3},
  {'city': 'Tasmania Region',
   'count': 614585,
   'country': 'AU',
   'locations': 34},
  {'city': 'South East Queensland',
   'count': 233852,
   'country': 'AU',
   'locations': 14},
  {'city': 'Sydney East', 'count': 351350, 'country': 'AU', 'locations': 6},
  {'city': 'Townsville', 'count': 10702, 'country': 'AU', 'locations': 2},
  {'city': 'Mt Isa', 'count': 16056, 'country': 'AU', 'locations': 2},
  {'city': 'Mackay', 'count': 10704, 'country': 'AU', 'locations': 2},
  {'city': 'Upper Hunter - Muswellbrook',
   'count': 61441,
   'country': 'AU',
   'locations': 1},
  {'city': 'Sydney North-west',
   'count': 247739,
   'country': 'AU',
   'locations': 4},
  {'city': 'Upper Hunter - Singleton',
   'count': 61717,
   'country': 'AU',
   'locations': 1},
  {'city': 'North-west Slopes',
   'count': 27988,
   'country': 'AU',
   'locations': 1},
  {'city': 'Central Coast', 'count': 93260, 'country': 'AU', 'locations': 1},
  {'city': 'Sarajevo', 'count': 189946, 'country': 'BA', 'locations': 5},
  {'city': 'Tuzla', 'count': 46007, 'country': 'BA', 'locations': 3},
  {'city': 'Zenica', 'count': 186009, 'country': 'BA', 'locations': 4},
  {'city': 'Kakanj', 'count': 16722, 'country': 'BA', 'locations': 1},
  {'city': 'Jajce', 'count': 48396, 'country': 'BA', 'locations': 1},
  {'city': 'Lukavac', 'count': 16132, 'country': 'BA', 'locations': 1},
  {'city': 'Goražde', 'count': 61652, 'country': 'BA', 'locations': 1},
  {'city': 'Živinice', 'count': 16339, 'country': 'BA', 'locations': 1},
  {'city': 'Dhaka', 'count': 15011, 'country': 'BD', 'locations': 2},
  {'city': 'Flanders', 'count': 592553, 'country': 'BE', 'locations': 67},
  {'city': 'Brussels', 'count': 127366, 'country': 'BE', 'locations': 9},
  {'city': 'Wallonia', 'count': 342899, 'country': 'BE', 'locations': 23},
  {'city': 'Antwerpen', 'count': 1170, 'country': 'BE', 'locations': 23},
  {'city': 'Limburg', 'count': 208, 'country': 'BE', 'locations': 5},
  {'city': 'West-Vlaanderen', 'count': 455, 'country': 'BE', 'locations': 9},
  {'city': 'Vlaams-Brabant', 'count': 371, 'country': 'BE', 'locations': 9},
  {'city': 'Brussels-Capital Region',
   'count': 751,
   'country': 'BE',
   'locations': 9},
  {'city': 'Oost-Vlaanderen', 'count': 697, 'country': 'BE', 'locations': 14},
  {'city': 'Liege', 'count': 1726, 'country': 'BE', 'locations': 8},
  {'city': 'Hainaut', 'count': 1772, 'country': 'BE', 'locations': 7},
  {'city': 'Brabant wallon', 'count': 212, 'country': 'BE', 'locations': 1},
  {'city': 'Namur', 'count': 628, 'country': 'BE', 'locations': 3},
  {'city': 'Luxembourg', 'count': 885, 'country': 'BE', 'locations': 4},
  {'city': 'Manama', 'count': 11566, 'country': 'BH', 'locations': 1},
  {'city': 'Americana', 'count': 30309, 'country': 'BR', 'locations': 1},
  {'city': 'Aracatuba', 'count': 29, 'country': 'BR', 'locations': 1},
  {'city': 'Sao Paulo', 'count': 323911, 'country': 'BR', 'locations': 55},
  {'city': 'Araçatuba', 'count': 30276, 'country': 'BR', 'locations': 1},
  {'city': 'Araraquara', 'count': 43185, 'country': 'BR', 'locations': 1},
  {'city': 'Bauru', 'count': 45259, 'country': 'BR', 'locations': 1},
  {'city': 'Campinas', 'count': 120419, 'country': 'BR', 'locations': 3},
  {'city': 'Campinas-V.Uniao', 'count': 43, 'country': 'BR', 'locations': 1},
  {'city': 'Capao Redondo', 'count': 33, 'country': 'BR', 'locations': 1},
  {'city': 'São Paulo', 'count': 699517, 'country': 'BR', 'locations': 15},
  {'city': 'Cap��o Redondo', 'count': 1, 'country': 'BR', 'locations': 1},
  {'city': 'Carapicuiba', 'count': 13639, 'country': 'BR', 'locations': 1},
  {'city': 'Carapicuíba', 'count': 8170, 'country': 'BR', 'locations': 1},
  {'city': 'Catanduva', 'count': 46347, 'country': 'BR', 'locations': 1},
  {'city': 'Cerqueira Cesar', 'count': 33, 'country': 'BR', 'locations': 1},
  {'city': 'Cerqueira César', 'count': 58953, 'country': 'BR', 'locations': 1},
  {'city': 'Cubatao-Centro', 'count': 43, 'country': 'BR', 'locations': 1},
  {'city': 'Cubatão', 'count': 133553, 'country': 'BR', 'locations': 3},
  {'city': 'Cubatao-Vale do Mogi',
   'count': 57,
   'country': 'BR',
   'locations': 1},
  {'city': 'Cubatão-Vila Parisi',
   'count': 7060,
   'country': 'BR',
   'locations': 1},
  {'city': 'Cubatao-V.Parisi', 'count': 43, 'country': 'BR', 'locations': 1},
  {'city': 'Diadema', 'count': 28667, 'country': 'BR', 'locations': 1},
  {'city': 'EM-50 Sesc Itaquera',
   'count': 14,
   'country': 'BR',
   'locations': 1},
  {'city': 'Guarulhos-Paco Municipal',
   'count': 43,
   'country': 'BR',
   'locations': 1},
  {'city': 'Guarulhos', 'count': 122909, 'country': 'BR', 'locations': 2},
  {'city': 'IPEN USP', 'count': 44, 'country': 'BR', 'locations': 1},
  {'city': 'Jacareí', 'count': 20171, 'country': 'BR', 'locations': 1},
  {'city': 'Jau', 'count': 43, 'country': 'BR', 'locations': 1},
  {'city': 'Jaú', 'count': 42798, 'country': 'BR', 'locations': 1},
  {'city': 'Jundiai', 'count': 43, 'country': 'BR', 'locations': 1},
  {'city': 'Jundiaí', 'count': 45328, 'country': 'BR', 'locations': 1},
  {'city': 'Limeira', 'count': 34104, 'country': 'BR', 'locations': 1},
  {'city': 'Marg.Tietê-Ponte dos Remédios',
   'count': 11015,
   'country': 'BR',
   'locations': 1},
  {'city': 'Marg.Tiete-Pte Remedios',
   'count': 66,
   'country': 'BR',
   'locations': 1},
  {'city': 'Marilia', 'count': 43, 'country': 'BR', 'locations': 1},
  {'city': 'Marília', 'count': 43140, 'country': 'BR', 'locations': 1},
  {'city': 'Mauá', 'count': 12372, 'country': 'BR', 'locations': 1},
  {'city': 'Nossa Senhora do Ó',
   'count': 4675,
   'country': 'BR',
   'locations': 1},
  {'city': 'N.Senhora do O', 'count': 30, 'country': 'BR', 'locations': 1},
  {'city': 'Osasco', 'count': 59172, 'country': 'BR', 'locations': 1}]}

3. Zurück zum Erdbeben API. Wie viele Erdbeben wurden im August 2017 verzeichnet?


In [16]:
url = 'https://earthquake.usgs.gov/fdsnws/event/1/count?starttime=2017-08-01T02:00:00&endtime=2017-09-01T02:00:00'

In [17]:
response = requests.get(url)
response.text


Out[17]:
'11800'

4. Und wie viele Erdbeben im Juli?


In [18]:
url = 'https://earthquake.usgs.gov/fdsnws/event/1/count?starttime=2017-07-01T02:00:00&endtime=2017-08-01T02:00:00'

In [19]:
response = requests.get(url)
response.text


Out[19]:
'12987'

 5. In welchem Monat des aktuellen Jahres wurden am meisten Erdbeben verzeichnet?


In [105]:
url = "https://earthquake.usgs.gov/fdsnws/event/1/count?format=geojson"

In [106]:
urltime = "&starttime=2017-01-01T02:00:00&endtime=2017-10-01T02:00:00"

In [107]:
response = requests.get(url+urltime)
response.text


Out[107]:
'{"count":95825,"maxAllowed":20000,"error":"95825 matching events exceeds search limit of 20000. A query using these search parameters would fail. Modify the search to match fewer events."}'

In [108]:
response.json()


Out[108]:
{'count': 95825,
 'error': '95825 matching events exceeds search limit of 20000. A query using these search parameters would fail. Modify the search to match fewer events.',
 'maxAllowed': 20000}

In [103]:
dct = response.json

 6. Wandle das in einen Pandas Dataframe um


In [ ]:


In [ ]:

7. Und speicher das Ergebnis ab


In [31]:


In [ ]: