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





