CSE 115. Introduction to Computer Science I

Similar documents
CSE 115. Introduction to Computer Science I

[301] JSON. Tyler Caraza-Harter

CSE 115. Introduction to Computer Science I

CSE 115. Introduction to Computer Science I

Web Scraping XML/JSON. Ben McCamish

Where is the Space Station?

JSON is a light-weight alternative to XML for data-interchange JSON = JavaScript Object Notation

Review files from last week:

BDS Query. JSON Query Syntax

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

Ninox API. Ninox API Page 1 of 15. Ninox Version Document version 1.0.0

Data. Notes. are required reading for the week. textbook reading and a few slides on data formats and data cleaning

JSON as an XML Alternative. JSON is a light-weight alternative to XML for datainterchange

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CIS 194: Homework 6. Due Friday, October 17, Preface. Setup. Generics. No template file is provided for this homework.

CSE 115. Introduction to Computer Science I

15-388/688 - Practical Data Science: Data collection and scraping. J. Zico Kolter Carnegie Mellon University Spring 2017

Introduction to JSON. Roger Lacroix MQ Technical Conference v

CSE 158/258. Web Mining and Recommender Systems. Tools and techniques for data processing and visualization

Files on disk are organized hierarchically in directories (folders). We will first review some basics about working with them.

Assigns a number to 110,000 letters/glyphs U+0041 is an A U+0062 is an a. U+00A9 is a copyright symbol U+0F03 is an

Grading for Assignment #1


See Types of Data Supported for information about the types of files that you can import into Datameer.

CSC Web Technologies, Spring Web Data Exchange Formats

Table of Contents. Developer Manual...1

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc.

JSON Support for Junos OS

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Assignment #3 CSCI 201 Spring % of course grade Title Weathermeister Back-End API Integration

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

ReJSON = { "activity": "new trick" } Itamar

MITOCW watch?v=kz7jjltq9r4

Introduction to Programming with Python. By: Victoria Kahian and Kyla Boswell

Chapter 1 - Consuming REST Web Services in Angular

Introduction to Bioinformatics

Large-Scale Networks

CIS192 Python Programming

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Tablo Documentation. Release Conservation Biology Institute

Python lab session 1

bottle-rest Release 0.5.0

REST. Web-based APIs

Types, lists & functions

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CIS192 Python Programming

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

JQuery and Javascript

CSE 115. Introduction to Computer Science I

Welcome to CS50 section! This is Week 10 :(

Clojure. The Revenge of Data. by Vjeran Marcinko Kapsch CarrierCom

Accessing Web Files in Python

Elliotte Rusty Harold August From XML to Flat Buffers: Markup in the Twenty-teens

Drexel Chatbot Requirements Specification

By Lucas Marshall. All materials Copyright Developer Shed, Inc. except where otherwise noted.

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Develop Mobile Front Ends Using Mobile Application Framework A - 2

Learning Objectives. Description. Your AU Expert(s) Trent Earley Behlen Mfg. Co. Shane Wemhoff Behlen Mfg. Co.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

PVB CONTACT FORM 7 CALCULATOR PRO DOCUMENTATION

Data Foundations. Topic Objectives. and list subcategories of each. its properties. before producing a visualization. subsetting

Web Component JSON Response using AppInventor

CSC 443: Web Programming

ARTIO SMS Services HTTP API Documentation

User Interaction: XML and JSON

10/18/2017. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Intro. Scheme Basics. scm> 5 5. scm>

CSE 115. Introduction to Computer Science I

Introduction to Web Scraping with Python

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Working with JavaScript

CSE 115. Introduction to Computer Science I

Input File Syntax The parser expects the input file to be divided into objects. Each object must start with the declaration:

Kyle Rainville Littleton Coin Company

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Making a Clickable Map Display

XML Processing & Web Services. Husni Husni.trunojoyo.ac.id

CS50 Quiz Review. November 13, 2017

Computer Science & Engineering 120 Learning to Code

CIS192 Python Programming

Lecture 4: Data Collection and Munging

If Statements, For Loops, Functions

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Data Collections. Welcome to the Fourth Dimension (and beyond) Martin Phillips Ladybridge Systems Ltd. International Spectrum Conference, 2014

Announcements. Two Classes of Database Applications. Class Overview. NoSQL Motivation. RDBMS Review: Serverless

JME Language Reference Manual

JavaScript Basics. The Big Picture

Comp 151. Control structures.

Data Analyst Nanodegree Syllabus

: Intro Programming for Scientists and Engineers Final Exam

DaMPL. Language Reference Manual. Henrique Grando

Piqi-RPC. Exposing Erlang services via JSON, XML and Google Protocol Buffers over HTTP. Friday, March 25, 2011

Google Earth. April CSCI Intro. to Comp. for the Humanities and Social Sciences 1

A bit more on Testing

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Transcription:

CSE 115 Introduction to Computer Science I

Road map Review HTTP Web API's JSON in Python Examples

Python Web Server import bottle @bottle.route("/") def any_name(): response = "<html><body><p>" response = response + "Hello from the server!" response = response + "</p></body></html>" return response bottle.run(host="0.0.0.0", port=8080, debug=true)

Python Web Server import bottle Import the library We had to install it first pip install --user bottle

Python Web Server bottle.run(host="0.0.0.0", port=8080, debug=true) Run the server on port 8080

Python Web Server @bottle.route("/") def any_name(): response = "<html><body><p>" response = response + "Hello from the server!" response = response + "</p></body></html>" return response Host content on the root path "/"

Web Server Client Sends requests to server Client Sends requests to server Client Sends requests to server Web Server Software runs continuously and waits for requests from clients Responds to requests

How do clients send requests to a server?

Road map Review HTTP Web API's JSON in Python Examples

HTTP We communicate with web servers by making HTTP requests The server will send an HTTP response in return The request is sent to a specific url in the format <protocol>://<server>/<path>?<query_string> Protocol: HTTP or HTTPS Server: The domain name for the server (eg. www.buffalo.edu) Path: Name for the resource being requests The path corresponds to the string in the annotation in bottle Query String: Provide additional info in key-value pairs Unused by most sites https://engineering.buffalo.edu/computer-science-engineering.html

import urllib.request HTTP Request url = "https://engineering.buffalo.edu/" url = url + "computer-science-engineering.html" response = urllib.request.urlopen(url) content = response.read().decode() print(content) Prints the HTML for CSE@UB's homepage

HTTP Request import urllib.request Import the urllib.request module Built-in to python No need to install

HTTP Request url = "https://engineering.buffalo.edu/" url = url + "computer-science-engineering.html" response = urllib.request.urlopen(url) content = response.read().decode() print(content) urllib.request contains the "urlopen" function that will setup an HTTP request to a provided url and return a response object The response can be read by calling read which returns a binary string Calling decode on the binary string converts it to a string Now do anything you can do with strings. For this example we only print the response to the screen

import urllib.request HTTP Request url = "https://engineering.buffalo.edu/" url = url + "computer-science-engineering.html" response = urllib.request.urlopen(url) content = response.read().decode() print(content)

Query Strings A set of key-value pairs key and value separated by "=" key-value pairs separated by "&" https://www.youtube.com/watch?v=5jmn_tbs0t4 Query String: "v=5jmn_tbs0t4" key "v" with value "5jmN_tBS0t4" https://www.google.com/search?q=cats&as_filetype=gif&lr=lang_ja Query String: "q=cats&as_filetype=gif&lr=lang_ja" key "q" with value "cats" key "as_filetype" with value "gif" key "lr" with value "lang_ja"

Query Strings import urllib.request url = "https://www.amazon.com" url = url + "/s?keywords=pens&sort=price-desc-rank" response = urllib.request.urlopen(url) content = response.read().decode() print(content) Sends an HTTPS request to the server "www.amazon.com" Requests the path "/s" With a query string containing 2 key key-value pairs key "keywords" with value "pens" key "sort" with value "price-desc-rank" Searches for the most expensive pens on amazon

Amazon Server Sends a Response I'll pass

Discussion The Internet, as most people know it, is designed for human consumption What if we want to write software that reads data from the Internet? How do we parse through the raw HTML in Python?

Web Scraping A web scraper is software that reads data from HTML. Many libraries exists to make this easier. We won't explore this in CSE115, though it can be a fun area to explore on your own.

Road map Review HTTP Web API's JSON in Python Examples

Web API The Internet, as most people know it, is designed for human consumption What if we want to write software that reads data from the Internet? Web APIs are hosted by web servers at urls, but instead of sending HTML/CSS/JavaScript they send raw data. Designed for programmatic consumption Typically send data as JSON

Web API import urllib.request url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content = response.read().decode() print(content) Connect to the open notify api Documentation: http://open-notify.org/open-notify-api/iss-location-now/ Returns a JSON String

Web API import urllib.request url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content = response.read().decode() print(content) { } "message": "success", "timestamp": 1540176365, "iss_position": { "latitude": "20.6716", "longitude": "-163.2050" }

Web API { } "message": "success", "timestamp": 1540176365, "iss_position": { "latitude": "20.6716", "longitude": "-163.2050" } We can get raw data, but what do we do with this JSON string? What is a JSON string?

Road map Review HTTP Web API's JSON in Python Examples

JSON JSON (JavaScript Object Notation) is a data format that can be represented as strings Send these strings to communicate across the Internet, and elsewhere All programming languages can read strings Doesn't matter what language was used to write the client or server program They can all "speak" JSON since its just strings More flexible than CSV

JSON Only 6 different data types String: Any value in "double quotes" Number: Any value not in quotes "true", "false", and "null" will be interpreted as a number. Should be formatted as an integer or floating point number Boolean: Either "true" or "false" without the quotes Null: The word "null" without the quotes Array: A comma-separated list of values surrounded by [brackets] Object: A comma-separated list of key-value pairs surrounded by {braces} [{"title":"god Am (Live 1996)","artist":"Alice in Chains","ratings": [5,4],"youtubeID":"74P4W_okEqA"},{"title":"Fade to Black","artist":"Metallica","ratings":[5,2],"youtubeID":"WEQnzs8wl6E"}] Closely resembles Javascript and Python syntax that we've seen, except it is a string. See e.g. https://www.json.org or http://www.ecma-international.org/ publications/files/ecma-st/ecma-404.pdf for more info.

import urllib.request import json JSON in Python url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content) Use the built-in json module to handle JSON strings Call json.loads to convert a JSON string to python types

` import urllib.request import json url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content) { } 'message': 'success', 'iss_position': { 'longitude': '-110.1453', 'latitude': '-39.6226'}, 'timestamp': 1540177620

import urllib.request import json JSON in Python url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content['iss_position']['longitude']) print(content['iss_position']['latitude']) The result looks similar to the JSON string, but now it is a Python dictionary instead of a string We can use dictionary functions to process the data

import urllib.request import json JSON in Python url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) content_string = response.read().decode() content = json.loads(content_string) print(content['iss_position']['longitude']) print(content['iss_position']['latitude']) -70.6226-51.3781 *Note: The numbers are different across slides since each time the code is executed we are getting the current location of the ISS. With web APIs we can work with live data!

Road map Review HTTP Web API's JSON in Python Examples

Want More? Big list of APIs https://github.com/toddmotto/public-apis Hackathon next week You have a lot of power at your fingertips with what we've covered so far! Build something great!