liveBook · Manning

Requests

As explained in The Quick Python Book, Fourth Edition

Requests is a Python library that provides a high-level interface for making HTTP requests.

requests is a Python library for fetching files and API data over HTTP or HTTPS. Install it with `pip install requests`, then import it and use an HTTP verb—usually GET—to retrieve a URL. For a one-off download, command-line tools such as curl or wget may be simpler; requests is useful when retrieval belongs inside a Python program. section 22.1.3 section 22.6

Fetch a text file

`import requests; response = requests.get(url)` retrieves a file served by a web server. Use `response.text` for the body as a string or `response.content` for bytes. You can save the result, clean it, or process it immediately. The response also includes server headers that may help with debugging. section 22.1.3

Fetching from an API uses the same `requests.get(url)` call. A query string after `?` selects data, with `&` separating parameters. For example, an Open-Meteo URL specifies latitude, longitude, temperature and wind speed. requests can quote spaces in a supplied URL before sending it, as illustrated by a date-range query to Chicago’s crime-data API. section 22.2

import requests
import csv
response = requests.get("https://archive-api.open-meteo.com/v1/era5?latitude=41.879&longitude=-87.64975
&start_date=2024-07-01&end_date=2024-07-31&daily=temperature_2m_mean")
weather_history = response.json()                            #1
 
weather_data = zip(weather_history["daily"]["time"],  
   weather_history["daily"]["temperature_2m_mean"])          #2
 
with open("weather_history.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Date", "Temperature"])
    writer.writerows(weather_data)                           #3
#1 The response object 's json() method returns data as JSON.
#2 The zip() function returns an iterator of the combined lists.
#3 The writerows() method writes all rows the from iterator weather_data.

A complete weather-data example calls `response.json()`, pairs parallel date and temperature lists with `zip()`, and writes the pairs to a CSV file. section 22.5.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Demonstrates GET, response bodies, JSON conversion, URL quoting and a CSV workflow without examining library internals.
Examples
Uses an airport temperature file, weather APIs and a Chicago crime-data API.
What it assumes
Assumes basic Python scripting and supplies the installation command and initial import-and-GET pattern.
What you can do after
Apply it via requests to retrieve web data, read text or JSON responses, and save processed results.
How it connects
Connects HTTP GET, query strings and JSON responses to CSV processing.

Also explained in

Compare how 6 books explain requests →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.