liveBook · Manning

Chapter 22: Data over the network

What you will learn in chapter 22 of The Quick Python Book, Fourth Edition

Introduces the chapter’s coverage of moving and obtaining data over a network, including file transfers, APIs, structured formats, and web scraping.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s coverage of moving and obtaining data over a network, including file transfers, APIs, structured formats, and web scraping.

22.1 Fetching files

This section explains when file retrieval is worth automating and how to decide whether Python is the right tool for it.

Key terms: data files, automate fetching, Python script, shell scripts, command-line tools, retrieval process

22.1.1 Using Python to fetch files from an FTP server

This subsection shows how to connect to an FTP server with Python, retrieve a file, and optionally use TLS encryption.

Key terms: ftplib, FTP object, anonymous, ftp.retrbinary, remote server, binary writing, write method, FTP, Transport Layer Security (TLS), FTP_TLS

22.1.2 Fetching files with SFTP

This subsection explains why SFTP is used for secure file transfers and how Python supports it through paramiko.

Key terms: SFTP, FTP, Secure Shell (SSH), paramiko, pip, SFTP/SCP client module, scp

22.1.3 Retrieving files over HTTP/HTTPS

This section shows how to retrieve a file from a web server with Python and access the returned data.

Key terms: HTTP/HTTPS, requests, GET, wget, curl, response body, text, content, string, bytes

22.2 Fetching data via an API

Explains how HTTP-based APIs serve data dynamically and how to fetch selected data with GET requests and the requests library.

Key terms: API, HTTP/HTTPS, static files, RESTful, GET request, query string, query string parameters, requests library, query parameters, quoting

22.3 Structured data formats

Explains why API data commonly uses structured text formats and identifies JSON and XML as the two most common examples.

Key terms: API, structured file format, JSON, XML, plain text

22.3.1 JSON data

This section explains how JSON represents data and how to parse, write, and organize JSON data with Python.

Key terms: JSON, structures, arrays, serialized, json.loads(), json.load(), requests, response object, json(), json.dump()

22.3.2 XML data

Explains why XML can be difficult to extract data from and how xmltodict turns it into Python dictionaries and lists.

Key terms: XML, elements, attributes, tree structure, metadata, xmltodict, parse, dictionary, @, #text

22.4 Scraping web data

This section introduces web scraping as fetching a page and extracting data from its HTML, using Beautiful Soup for a simple example while cautioning about legality and site load.

Key terms: scraping, crawling, requests, Beautiful Soup, parser object, HTML tag, select, class, terms of use, denial-of-service attack

22.5 Tracking the weather

This section sets a task to retrieve a month of daily mean temperatures from the Open-Meteo archive API and reshape the results for spreadsheet graphing.

Key terms: archive API, latitude, longitude, start_date, end_date, daily=temperature_2m_mean

22.5.1 Solving the problem with AI-generated code

This subsection explains why the weather-tracking lab is a suitable task for AI-generated code.

Key terms: HTTP API, JSON, AI

22.5.2 Solutions and discussion

22.6 Summary

Concepts explained in this chapter

← Chapter 21: Processing data files Chapter 23: Saving data →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.