Download presentation
Presentation is loading. Please wait.
1
Intro to Web Services Consuming the Web
2
Introduction to Web Clients
We've seen how to write server-side web applications Today's focus: Web Clients A web client is a program that interacts with a server-side web application called a web service A web service is a server-side web application that is designed to be accessed by programs rather than humans
3
Meet the requests module
Third-party Python module for downloading web pages Install: pip install requests Use: import requests response = requests.get(' print(response.text) requests Documentation:
4
Requests API The get() method returns a Response object Properties:
response = requests.get(' Properties: status_code 200 – ok 404 – not found text (downloaded data) Methods: json()
5
Downloading Data Websites publish data in different formats
HTML Media files CSV XML JSON Use the requests module to download any type of data Not just web pages
6
Downloading CSV Data Hartford fire data in CSV format
Process with csv reader response = requests.get(' lines = response.text.split('\n') reader = csv.reader(lines[1:]) for line in reader: alm_time = line[2] arv_time = line[4] print(alm_time, arv_time)
7
JSON
8
JSON Common data interchange format More powerful/flexible than CSV
Example: {"menu": { "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, ] } }}
9
Parsing JSON Use json module (built-in)
json.loads() function converts a string containing JSON to a Python list/dictionary See examples/dictionaries_csv/fires.py
10
Downloading JSON Data requests module has built-in support for parsing JSON See examples/dictionaries_csv/fireswebservice.py
11
Web Services
12
Hartford Fire Data Consider this URL:
We can control the downloaded data via a query parameter What does that tell you about the nature of this URL? Static vs. Dynamic?
13
API Recall: An interface is the boundary between a client and a mechanism used by the client Input flows from the client through the interface to the mechanism Output flows from the mechanism through the interface back to the client API: "Application Program Interface" Python API = Function interfaces + Documentation Example: requests API, file API, string API
14
Web API Web applications get their input via
URL Query strings Submitted form data Cookies A web service provides a web API that specifies format of URL and query strings that control what data is downloaded
15
Hartford Fire Data Web API
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.