{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "CDSW Week 2: Using APIs to Get Data From the Internet\n", "==============================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Agenda\n", "------\n", "\n", "**Last session** we learned some basic command in python\n", "\n", "* Variables\n", "* Types\n", "* lists\n", "* dicts\n", "* for loops\n", "\n", "**This week** we will learn how to find and retrieve data from the internet\n", "\n", "**Next session** we will learn to do some Data Science on our data.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Warmup\n", "------\n", "\n", "**Lists of dictionaries**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a dictionary with age, town, and state." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a list of all my siblings " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# iterate through the list and print the age and state (using f'')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Creating strings from variables*\n", "\n", " print(f'Inside the curly braces is python: {1 + 1}')\n", " \n", "*Nesting dictionaries in lists*\n", "\n", "You can use lists as the values in dictionaries. \n", "\n", "You can use lists in lists!\n", "\n", "You can use dictionaries in lists!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting Data from APIs\n", "\n", "\n", "**API** means Application Programmer Interface\n", "\n", "An API is a set of instructions that describe how computers can interact with each other to request and receive information.\n", "\n", "Some important questions we will ask that help us discover APIs is below.\n", "\n", "|Question | In technical terms |\n", "|:---------|:--------------------|\n", "|Where is my data? | What is the domain? |\n", "|How do I learn what data is available?| Where is the documentation? |\n", "|How do I request specific data?| How do I formulate a URL for a specific purpose? |\n", "|How do I interpret the data?| What is the structure and format of the output?|\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Let's walk through an example in the browser**\n", "\n", "PlaceKitten!\n", "\n", "In a browser, go to http://www.placekitten.com\n", "\n", "|In technical terms | PlaceKitten |\n", "|:---------|:--------------------|\n", "|What is the domain? | http://www.placekitten.com |\n", "|Where is the documentation?| The documentation is on the home page. |\n", "|How do I formulate a URL for a specific purpose? | You put it in the url like http://www.placekitten/width/height |\n", "|What is the structure and format of the output?| It's an image! |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Accessing placekitten in python\n", "\n", "We're going to use a special library called requests" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Image # This line lets you display images. We'll use that in a bit.\n", "\n", "\n", "# This line lets you use python to download data from the web.\n", "import requests" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get a 200 by 300 image from placekitten.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Look at the status code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# print the content\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use the Image function to display the image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use input to request width/height and print an image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* What happens when you add \"g\" to the URL?\n", "\n", "* Can you write a loop to show several images?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2: Getting World Times\n", "\n", "This example introduces a slightly more complicated API. It also introduces **JSON** which is a very common data format.\n", "\n", "Our API is at http://worldtimeapi.org/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Download list of time zones\n", "r = requests.get(\"http://worldtimeapi.org/api/timezone\")\n", "print(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use the .json() function to get the response as a list or dict\n", "# What did it return?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get the time for your time zone (America/Los_Angeles)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get the time for your IP address" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This Afternoon\n", "\n", "We need a show of hands for who wants to attend each session:\n", "\n", "* Yelp data\n", "\n", "* Twitter (only if you already did the homework!)\n", "\n", "* Wikipedia" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }