.. _TD2AecolesAPIrst: ====================== 2A.eco - API, API REST ====================== .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/td2a_eco/TD2A_eco_les_API.ipynb|*` Petite revue d’\ `API REST `__. .. code:: from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: Définition : ------------ API, à part que ce mot qui vaut 5 au scrabble, c’est quoi au juste ? API signifie Application Programming Interface. Le mot le plus important est “interface”, et c’est le mot le plus simple, car nous utilisons tous des interfaces. Bon, et une interface ? Définition Larrouse : “Une interface est un dispositif qui permet des échanges et interactions entre différents acteurs” Pour faire simple, une API est un moyen efficace de faire communiquer entre elles deux applications : concrètement, un fournisseur de service met à disposition des développeurs une interface codifiée, qui leur permet d’obtenir des informations à partir de requêtes. Sans rentrer dans le détail technique, le dialogue ressemble à : “envoie moi ton adresse sous la forme X = rue, Y = Ville, Z = Pays” et moi, en retour, je t’enverrai le code à afficher sur ton site pour avoir la carte interactive. Les API qui existent -------------------- De plus en plus de sites mettent à disposition des développeurs et autres curieux des API. Pour en citer quelques-uns : - Twitter : https://dev.twitter.com/rest/public - Facebook : https://developers.facebook.com/ - Instagram : https://www.instagram.com/developer/ - Spotify : https://developer.spotify.com/web-api/ Ou encore : - Pole Emploi : https://www.emploi-store-dev.fr/portail-developpeur-cms/home.html - SNCF : https://data.sncf.com/api - AirFrance KLM : https://developer.airfranceklm.com/Our_Apis - Banque Mondiale : https://datahelpdesk.worldbank.org/knowledgebase/topics/125589 Comment parler à une API ? -------------------------- La plupart des API donnent des exemples par communiquer avec les données présentes sur le site. Simplement, il faut trouver l’url qui renvoit les données que vous souhaitez avoir Par exemple, avec l’API de la Banque mondiale, voici comme s’écrit une requête pour les données de la Banque Mondiale : http://api.worldbank.org/countries?incomeLevel=LMC Avec cette url, on demande la liste des pays dont le niveau de revenus est LMC, c’est à dire “Lower middle income”. En cliquant sur le lien, le site renvoit des données en XML, qui ressemblent pas mal à ce qu’on a vu plus tôt avec le scraping : une structure avec des balises qui s’ouvrent et qui se ferment. AM Armenia Europe & Central Asia Europe & Central Asia (excluding high income) Lower middle income IBRD Yerevan 44.509 40.1596 BD Bangladesh South Asia South Asia Lower middle income IDA Dhaka 90.4113 23.7055 ..... Quand on regare de plus près, on voit que les informations suivantes apparaissent Code du pays \| Nom du pays \| Région \| Classification en termes de revenus \| Les types de prêt pour ces pays \| La capitale \| Longitude \| Latitude -------------- AM Armenia Europe & Central Asia Europe & Central Asia (excluding high income) Lower middle income IBRD Yerevan 44.509 40.1596 BD Bangladesh South Asia South Asia Lower middle income IDA Dhaka 90.4113 23.7055 En utilisant cette url ci : http://api.worldbank.org/countries?incomeLevel=LMC&format=json, on obtient directement un json, qui est finalement presque comme un dictionnaire en python. Rien de plus simple donc pour demander quelque chose à une API, il suffit d’avoir la bonne url. Et Python : comment il s’adresse aux API ? ------------------------------------------ C’est là qu’on revient aux fondamentaux : on va avoir besoin du module requests de Python et suivant les API, un parser comme BeautifulSoup ou rien si on réussit à obtenir un json. On va utiliser le module requests et sa méthode get : on lui donne l’url de l’API qui nous intéresse, on lui demande d’en faire un json et le tour est joué ! Faire appel à l’API de la Banque Mondiale ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code:: import requests data_json = requests.get("http://api.worldbank.org/v2/countries?incomeLevel=LMC&format=json").json() data_json .. parsed-literal:: [{'page': 1, 'pages': 1, 'per_page': '50', 'total': 47}, [{'id': 'AGO', 'iso2Code': 'AO', 'name': 'Angola', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Luanda', 'longitude': '13.242', 'latitude': '-8.81155'}, {'id': 'BGD', 'iso2Code': 'BD', 'name': 'Bangladesh', 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Dhaka', 'longitude': '90.4113', 'latitude': '23.7055'}, {'id': 'BOL', 'iso2Code': 'BO', 'name': 'Bolivia', 'region': {'id': 'LCN', 'iso2code': 'ZJ', 'value': 'Latin America & Caribbean '}, 'adminregion': {'id': 'LAC', 'iso2code': 'XJ', 'value': 'Latin America & Caribbean (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'La Paz', 'longitude': '-66.1936', 'latitude': '-13.9908'}, {'id': 'BTN', 'iso2Code': 'BT', 'name': 'Bhutan', 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Thimphu', 'longitude': '89.6177', 'latitude': '27.5768'}, {'id': 'CIV', 'iso2Code': 'CI', 'name': "Cote d'Ivoire", 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Yamoussoukro', 'longitude': '-4.0305', 'latitude': '5.332'}, {'id': 'CMR', 'iso2Code': 'CM', 'name': 'Cameroon', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Yaounde', 'longitude': '11.5174', 'latitude': '3.8721'}, {'id': 'COG', 'iso2Code': 'CG', 'name': 'Congo, Rep.', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Brazzaville', 'longitude': '15.2662', 'latitude': '-4.2767'}, {'id': 'COM', 'iso2Code': 'KM', 'name': 'Comoros', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Moroni', 'longitude': '43.2418', 'latitude': '-11.6986'}, {'id': 'CPV', 'iso2Code': 'CV', 'name': 'Cabo Verde', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Praia', 'longitude': '-23.5087', 'latitude': '14.9218'}, {'id': 'DJI', 'iso2Code': 'DJ', 'name': 'Djibouti', 'region': {'id': 'MEA', 'iso2code': 'ZQ', 'value': 'Middle East & North Africa'}, 'adminregion': {'id': 'MNA', 'iso2code': 'XQ', 'value': 'Middle East & North Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Djibouti', 'longitude': '43.1425', 'latitude': '11.5806'}, {'id': 'EGY', 'iso2Code': 'EG', 'name': 'Egypt, Arab Rep.', 'region': {'id': 'MEA', 'iso2code': 'ZQ', 'value': 'Middle East & North Africa'}, 'adminregion': {'id': 'MNA', 'iso2code': 'XQ', 'value': 'Middle East & North Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Cairo', 'longitude': '31.2461', 'latitude': '30.0982'}, {'id': 'FSM', 'iso2Code': 'FM', 'name': 'Micronesia, Fed. Sts.', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Palikir', 'longitude': '158.185', 'latitude': '6.91771'}, {'id': 'GHA', 'iso2Code': 'GH', 'name': 'Ghana', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Accra', 'longitude': '-0.20795', 'latitude': '5.57045'}, {'id': 'HND', 'iso2Code': 'HN', 'name': 'Honduras', 'region': {'id': 'LCN', 'iso2code': 'ZJ', 'value': 'Latin America & Caribbean '}, 'adminregion': {'id': 'LAC', 'iso2code': 'XJ', 'value': 'Latin America & Caribbean (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Tegucigalpa', 'longitude': '-87.4667', 'latitude': '15.1333'}, {'id': 'IDN', 'iso2Code': 'ID', 'name': 'Indonesia', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Jakarta', 'longitude': '106.83', 'latitude': '-6.19752'}, {'id': 'IND', 'iso2Code': 'IN', 'name': 'India', 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'New Delhi', 'longitude': '77.225', 'latitude': '28.6353'}, {'id': 'KEN', 'iso2Code': 'KE', 'name': 'Kenya', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Nairobi', 'longitude': '36.8126', 'latitude': '-1.27975'}, {'id': 'KGZ', 'iso2Code': 'KG', 'name': 'Kyrgyz Republic', 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'}, 'adminregion': {'id': 'ECA', 'iso2code': '7E', 'value': 'Europe & Central Asia (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Bishkek', 'longitude': '74.6057', 'latitude': '42.8851'}, {'id': 'KHM', 'iso2Code': 'KH', 'name': 'Cambodia', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Phnom Penh', 'longitude': '104.874', 'latitude': '11.5556'}, {'id': 'KIR', 'iso2Code': 'KI', 'name': 'Kiribati', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Tarawa', 'longitude': '172.979', 'latitude': '1.32905'}, {'id': 'LAO', 'iso2Code': 'LA', 'name': 'Lao PDR', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Vientiane', 'longitude': '102.177', 'latitude': '18.5826'}, {'id': 'LSO', 'iso2Code': 'LS', 'name': 'Lesotho', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Maseru', 'longitude': '27.7167', 'latitude': '-29.5208'}, {'id': 'MAR', 'iso2Code': 'MA', 'name': 'Morocco', 'region': {'id': 'MEA', 'iso2code': 'ZQ', 'value': 'Middle East & North Africa'}, 'adminregion': {'id': 'MNA', 'iso2code': 'XQ', 'value': 'Middle East & North Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Rabat', 'longitude': '-6.8704', 'latitude': '33.9905'}, {'id': 'MDA', 'iso2Code': 'MD', 'name': 'Moldova', 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'}, 'adminregion': {'id': 'ECA', 'iso2code': '7E', 'value': 'Europe & Central Asia (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Chisinau', 'longitude': '28.8497', 'latitude': '47.0167'}, {'id': 'MMR', 'iso2Code': 'MM', 'name': 'Myanmar', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Naypyidaw', 'longitude': '95.9562', 'latitude': '21.914'}, {'id': 'MNG', 'iso2Code': 'MN', 'name': 'Mongolia', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Ulaanbaatar', 'longitude': '106.937', 'latitude': '47.9129'}, {'id': 'MRT', 'iso2Code': 'MR', 'name': 'Mauritania', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Nouakchott', 'longitude': '-15.9824', 'latitude': '18.2367'}, {'id': 'NGA', 'iso2Code': 'NG', 'name': 'Nigeria', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Abuja', 'longitude': '7.48906', 'latitude': '9.05804'}, {'id': 'NIC', 'iso2Code': 'NI', 'name': 'Nicaragua', 'region': {'id': 'LCN', 'iso2code': 'ZJ', 'value': 'Latin America & Caribbean '}, 'adminregion': {'id': 'LAC', 'iso2code': 'XJ', 'value': 'Latin America & Caribbean (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Managua', 'longitude': '-86.2734', 'latitude': '12.1475'}, {'id': 'PAK', 'iso2Code': 'PK', 'name': 'Pakistan', 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Islamabad', 'longitude': '72.8', 'latitude': '30.5167'}, {'id': 'PHL', 'iso2Code': 'PH', 'name': 'Philippines', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Manila', 'longitude': '121.035', 'latitude': '14.5515'}, {'id': 'PNG', 'iso2Code': 'PG', 'name': 'Papua New Guinea', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Port Moresby', 'longitude': '147.194', 'latitude': '-9.47357'}, {'id': 'PSE', 'iso2Code': 'PS', 'name': 'West Bank and Gaza', 'region': {'id': 'MEA', 'iso2code': 'ZQ', 'value': 'Middle East & North Africa'}, 'adminregion': {'id': 'MNA', 'iso2code': 'XQ', 'value': 'Middle East & North Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'LNX', 'iso2code': 'XX', 'value': 'Not classified'}, 'capitalCity': '', 'longitude': '', 'latitude': ''}, {'id': 'SDN', 'iso2Code': 'SD', 'name': 'Sudan', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Khartoum', 'longitude': '32.5363', 'latitude': '15.5932'}, {'id': 'SEN', 'iso2Code': 'SN', 'name': 'Senegal', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Dakar', 'longitude': '-17.4734', 'latitude': '14.7247'}, {'id': 'SLB', 'iso2Code': 'SB', 'name': 'Solomon Islands', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Honiara', 'longitude': '159.949', 'latitude': '-9.42676'}, {'id': 'SLV', 'iso2Code': 'SV', 'name': 'El Salvador', 'region': {'id': 'LCN', 'iso2code': 'ZJ', 'value': 'Latin America & Caribbean '}, 'adminregion': {'id': 'LAC', 'iso2code': 'XJ', 'value': 'Latin America & Caribbean (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'San Salvador', 'longitude': '-89.2073', 'latitude': '13.7034'}, {'id': 'STP', 'iso2Code': 'ST', 'name': 'Sao Tome and Principe', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Sao Tome', 'longitude': '6.6071', 'latitude': '0.20618'}, {'id': 'SWZ', 'iso2Code': 'SZ', 'name': 'Eswatini', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Mbabane', 'longitude': '31.4659', 'latitude': '-26.5225'}, {'id': 'TLS', 'iso2Code': 'TL', 'name': 'Timor-Leste', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Dili', 'longitude': '125.567', 'latitude': '-8.56667'}, {'id': 'TUN', 'iso2Code': 'TN', 'name': 'Tunisia', 'region': {'id': 'MEA', 'iso2code': 'ZQ', 'value': 'Middle East & North Africa'}, 'adminregion': {'id': 'MNA', 'iso2code': 'XQ', 'value': 'Middle East & North Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Tunis', 'longitude': '10.21', 'latitude': '36.7899'}, {'id': 'UKR', 'iso2Code': 'UA', 'name': 'Ukraine', 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'}, 'adminregion': {'id': 'ECA', 'iso2code': '7E', 'value': 'Europe & Central Asia (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Kiev', 'longitude': '30.5038', 'latitude': '50.4536'}, {'id': 'UZB', 'iso2Code': 'UZ', 'name': 'Uzbekistan', 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'}, 'adminregion': {'id': 'ECA', 'iso2code': '7E', 'value': 'Europe & Central Asia (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Tashkent', 'longitude': '69.269', 'latitude': '41.3052'}, {'id': 'VNM', 'iso2Code': 'VN', 'name': 'Vietnam', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Hanoi', 'longitude': '105.825', 'latitude': '21.0069'}, {'id': 'VUT', 'iso2Code': 'VU', 'name': 'Vanuatu', 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'}, 'adminregion': {'id': 'EAP', 'iso2code': '4E', 'value': 'East Asia & Pacific (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Port-Vila', 'longitude': '168.321', 'latitude': '-17.7404'}, {'id': 'ZMB', 'iso2Code': 'ZM', 'name': 'Zambia', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'}, 'capitalCity': 'Lusaka', 'longitude': '28.2937', 'latitude': '-15.3982'}, {'id': 'ZWE', 'iso2Code': 'ZW', 'name': 'Zimbabwe', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'}, 'capitalCity': 'Harare', 'longitude': '31.0672', 'latitude': '-17.8312'}]] .. code:: data_json[0] # On voit qu'il y a nous manque des informations : # il y a un total de 52 éléments data_json_page_2 = requests.get("http://api.worldbank.org/v2/countries?incomeLevel=LMC&format=json&page=2").json() data_json_page_2 .. parsed-literal:: [{'page': 2, 'pages': 1, 'per_page': '50', 'total': 47}, []] .. code:: # pour obtenir une observation # on voit dans l'objet que l'élément 0 correspond à des informations sur les pages data_json[1][0] .. parsed-literal:: {'id': 'AGO', 'iso2Code': 'AO', 'name': 'Angola', 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '}, 'adminregion': {'id': 'SSA', 'iso2code': 'ZF', 'value': 'Sub-Saharan Africa (excluding high income)'}, 'incomeLevel': {'id': 'LMC', 'iso2code': 'XN', 'value': 'Lower middle income'}, 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'}, 'capitalCity': 'Luanda', 'longitude': '13.242', 'latitude': '-8.81155'} Faire appel à l’API de Tastekid ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ La Banque Mondiale c’était assez soft : on va passer sur du un peu plus costaud. On va utiliser l’API de Tastekid, site de recommandations de films, livres etc. Pour cela, il faut commencer par créer un compte : .. code:: import os from pyquickhelper.loghelper import get_password key = get_password("tastekid", "ensae_teaching_cs,key") if key is None: raise ValueError("password cannot be None.") Pour demander à l’API quels sont les oeuvres similaires à Pulp Fiction, nous utilisons la requête suivante .. code:: url = "https://tastedive.com/api/similar?q=pulp+fiction&info=1&k={}".format(key) recommandations_res = requests.get(url) if "401 Unauthorized" in recommandations_res.text: print("Le site tastekid n'accepte pas les requêtes non authentifiée.") print(recommandations_res.text) recommandations_res = None if recommandations_res is not None: try: recommandations = recommandations_res.json() except Exception as e: print(e) # Parfois le format json est mal formé. On regarde pourquoi. print() raise Exception(recommandations_res.text) from e .. code:: if recommandations_res is not None: print(str(recommandations)[:2000]) .. parsed-literal:: {'Similar': {'Info': [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'Pulp Fiction is a 1994 American crime film written and directed by Quentin Tarantino, who conceived it with Roger Avary. Starring John Travolta, Samuel L. Jackson, Bruce Willis, Tim Roth, Ving Rhames, and Uma Thurman, it tells several stories of criminal Los Angeles. The title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.Tarantino wrote Pulp Fiction in 1992 and 1993, incorporating scenes that Avary originally wrote for True Romance (1993). Its plot occurs out of chronological order. The film is also self-referential from its opening moments, beginning with a title card that gives two dictionary definitions of "pulp". Considerable screen time is devoted to monologues and casual conversations with eclectic dialogue revealing each character\'s perspectives on several subjects, and the film features an ironic combination of humor and strong violence. TriStar Pictures reportedly turned down the script as "too demented". Miramax co-chairman Harvey Weinstein was enthralled, however, and the film became the first that Miramax fully financed.', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}], 'Results': [{'Name': 'Fight Club', 'Type': 'movie', 'wTeaser': 'Fight Club is a 1999 American film directed by David Fincher and starring Brad Pitt, Edward Norton, and Helena Bonham Carter. It is based on the 1996 novel of the same name by Chuck Palahniuk. Norton plays the unnamed narrator, who is discontented with his white-collar job. He forms a "fight club" with soap salesman Tyler Durden (Pitt), and becomes embroiled in a relationship with him and a destitute woman, Marla Singer (Bonham Carter).Palahniuk\'s novel was optioned by Fox 2000 Pictures producer Laura Ziskin, who hired Jim Uhls to write the film adapta .. code:: # on nous rappelle les informations sur l'élement que l'on recherche : Pulp Fiction recommandations['Similar']['Info'] .. parsed-literal:: [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'Pulp Fiction is a 1994 American crime film written and directed by Quentin Tarantino, who conceived it with Roger Avary. Starring John Travolta, Samuel L. Jackson, Bruce Willis, Tim Roth, Ving Rhames, and Uma Thurman, it tells several stories of criminal Los Angeles. The title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.Tarantino wrote Pulp Fiction in 1992 and 1993, incorporating scenes that Avary originally wrote for True Romance (1993). Its plot occurs out of chronological order. The film is also self-referential from its opening moments, beginning with a title card that gives two dictionary definitions of "pulp". Considerable screen time is devoted to monologues and casual conversations with eclectic dialogue revealing each character\'s perspectives on several subjects, and the film features an ironic combination of humor and strong violence. TriStar Pictures reportedly turned down the script as "too demented". Miramax co-chairman Harvey Weinstein was enthralled, however, and the film became the first that Miramax fully financed.', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}] .. code:: # on nous donnes des livres / filmes proches selon le gout des gens for element in recommandations['Similar']['Results'] : print(element['Name'],element['Type']) .. parsed-literal:: Fight Club movie Seven movie The Silence Of The Lambs movie The Matrix movie Inglourious Basterds movie American History X movie Django Unchained movie Reservoir Dogs movie The Shawshank Redemption movie Memento movie One Flew Over The Cuckoo's Nest movie The Godfather movie The Dark Knight movie Forrest Gump movie Goodfellas movie Léon: The Professional movie The Usual Suspects movie The Green Mile movie The Shining movie The Departed movie On peut aussi ne demander que des films, on ajoute juste l’option type = movies dans l’url .. code:: recommandations_films = requests.get("https://tastedive.com/api/similar?q=pulp+fiction&type=movie&info=1&k={}" .format(key)).json() print(str(recommandations_films)[:2000]) .. parsed-literal:: {'Similar': {'Info': [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'Pulp Fiction is a 1994 American crime film written and directed by Quentin Tarantino, who conceived it with Roger Avary. Starring John Travolta, Samuel L. Jackson, Bruce Willis, Tim Roth, Ving Rhames, and Uma Thurman, it tells several stories of criminal Los Angeles. The title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.Tarantino wrote Pulp Fiction in 1992 and 1993, incorporating scenes that Avary originally wrote for True Romance (1993). Its plot occurs out of chronological order. The film is also self-referential from its opening moments, beginning with a title card that gives two dictionary definitions of "pulp". Considerable screen time is devoted to monologues and casual conversations with eclectic dialogue revealing each character\'s perspectives on several subjects, and the film features an ironic combination of humor and strong violence. TriStar Pictures reportedly turned down the script as "too demented". Miramax co-chairman Harvey Weinstein was enthralled, however, and the film became the first that Miramax fully financed.', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}], 'Results': [{'Name': 'Fight Club', 'Type': 'movie', 'wTeaser': 'Fight Club is a 1999 American film directed by David Fincher and starring Brad Pitt, Edward Norton, and Helena Bonham Carter. It is based on the 1996 novel of the same name by Chuck Palahniuk. Norton plays the unnamed narrator, who is discontented with his white-collar job. He forms a "fight club" with soap salesman Tyler Durden (Pitt), and becomes embroiled in a relationship with him and a destitute woman, Marla Singer (Bonham Carter).Palahniuk\'s novel was optioned by Fox 2000 Pictures producer Laura Ziskin, who hired Jim Uhls to write the film adapta .. code:: # on nous donnes des livres / filmes proches selon le gout des gens for element in recommandations_films['Similar']['Results'] : print(element['Name'],element['Type']) .. parsed-literal:: Fight Club movie Seven movie The Silence Of The Lambs movie The Matrix movie Inglourious Basterds movie American History X movie Django Unchained movie Reservoir Dogs movie The Shawshank Redemption movie Memento movie One Flew Over The Cuckoo's Nest movie The Godfather movie The Dark Knight movie Forrest Gump movie Goodfellas movie Léon: The Professional movie The Usual Suspects movie The Green Mile movie The Shining movie The Departed movie .. code:: film_suivant = "Reservoir Dogs" recommandations_suivantes_films = requests.get( "https://tastedive.com/api/similar?q={}&type=movie&info=1&k={}" .format(film_suivant, key)).json() # on nous donnes des livres / filmes proches selon le gout des gens for element in recommandations_suivantes_films['Similar']['Results'] : print(element['Name'],element['Type']) .. parsed-literal:: American History X movie Pulp Fiction movie The Usual Suspects movie No Country For Old Men movie Full Metal Jacket movie Snatch movie Inglourious Basterds movie Memento movie The Big Lebowski movie Fight Club movie Seven movie Goodfellas movie Lock, Stock And Two Smoking Barrels movie American Beauty movie Fargo movie Kill Bill movie One Flew Over The Cuckoo's Nest movie A Clockwork Orange movie Scarface movie The Departed movie .. code:: ## On peut ensuite comparer les films communs aux deux recherches liste1 = [element['Name'] for element in recommandations_films['Similar']['Results'] ] liste2 = [element['Name'] for element in recommandations_suivantes_films['Similar']['Results'] ] films_commun = set(liste1).intersection(liste2) films_commun, len(films_commun) .. parsed-literal:: ({'American History X', 'Fight Club', 'Goodfellas', 'Inglourious Basterds', 'Memento', "One Flew Over The Cuckoo's Nest", 'Seven', 'The Departed', 'The Usual Suspects'}, 9) .. code:: films_non_partages = [f for f in liste1 if f not in liste2] + [f for f in liste2 if f not in liste1] films_non_partages .. parsed-literal:: ['The Silence Of The Lambs', 'The Matrix', 'Django Unchained', 'Reservoir Dogs', 'The Shawshank Redemption', 'The Godfather', 'The Dark Knight', 'Forrest Gump', 'Léon: The Professional', 'The Green Mile', 'The Shining', 'Pulp Fiction', 'No Country For Old Men', 'Full Metal Jacket', 'Snatch', 'The Big Lebowski', 'Lock, Stock And Two Smoking Barrels', 'American Beauty', 'Fargo', 'Kill Bill', 'A Clockwork Orange', 'Scarface'] A partir de ces requêtes, on peut facilement construire le réseau des films que les gens aiment en utilisant le package `networkx `__.