Statistics
8
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-03-24

AuthorAl Sweigart

Also check out the new workbook with exercises: Automate the Boring Stuff with Python Workbook The bestselling introduction to Python programming, revised to include the latest Python features, improved explanations, and new chapters about databases and sound files. If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do this work for you? In this fully revised third edition of Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Early chapters will teach you the fundamentals of Python through clear explanations and engaging examples. You’ll write your first Python program; work with strings, lists, dictionaries, and other data structures; then use regular expressions to find and manipulate text patterns. Once you’ve mastered the basics, you’ll tackle projects that teach you to use Python to automate tasks like: • Searching the web, downloading content, and filling out forms • Finding, extracting, and manipulating text and data in files and spreadsheets • Copying, moving, renaming, or compressing saved files on your computer • Splitting, merging, and extracting text from PDFs and Word documents • Interacting with applications through custom mouse and keyboard macros • Managing your inbox, unsubscribing from lists, and sending email or text notifications New to this edition: All code and examples have been thoroughly updated. You’ll also find four new chapters on database integration, speech recognition, and audio and video editing, as well as 16 new programming projects and expanded coverage of developer techniques like creating command line programs.

Tags
No tags
ISBN: 1718503415
Publisher: No Starch Press
Publish Year: 2025
Language: 英文
Pages: 675
File Format: PDF
File Size: 8.6 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
(This page has no text content)
PRAISE FOR AUTOMATE THE BORING STUFF WITH PYTHON “If you are new to Python, get this book. . . . If you lie in the category of experienced Pythonistas and haven’t read this book, you should also take out some time and read it.” —the startup, medium “I’m having a lot of fun breaking things and then putting them back together, and just remembering the joy of turning a set of instructions into something useful and fun, like I did when I was a kid.” — wil wheaton, actor, writer, and geek icon “Do you need Automate the Boring Stuff with Python? Yes, if you want to enhance your workflow by using automation, this is an excellent place to start. Highly recommended.” —mark gibbs, network world “The best part of programming is the triumph of seeing the machine do something useful. Automate the Boring Stuff with Python frames all of programming as these small triumphs; it makes the boring fun.” — hilary mason, founder of fast forward labs and data scientist in residence at accel “Automate the Boring Stuff with Python is perfect for anyone who has menial tasks they don’t want to spend hours doing.” —dakster sullivan, geekmom “Whether you prefer working through a book, or learning by watching, or both together, Automate the Boring Stuff with Python will have you productive in Python in no time.” —serdar regulalp, info world
(This page has no text content)
® A U T O M AT E T H E B O R I N G S T U F F W I T H P Y T H O N 3 r d   E d i t i o n P r a c t i c a l P r o g r a m m i n g f o r To t a l B e g i n n e r s by Al Sweigar t San Francisco
[E] AUTOMATE THE BORING STUFF WITH PYTHON, 3RD EDITION. Copyright © 2025 by Al Sweigart. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license. To view a copy of this license, visit https://creativecommons.org/ licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. Some rights reserved. When attributing this work, you must credit the author as follows: “Al Sweigart, published by No Starch Press® Inc.,” provide a link to the license, and indicate if changes were made. You may not use the material for commercial purposes. For ShareAlike purposes, if you transform or build upon the material, you must distribute your contributions under the same license as the original. Translations of this work are not covered under this license; all translation rights are reserved by the publisher. For permission to translate this work, please contact rights@nostarch.com. Moral rights of the author have been asserted. First printing ISBN-13: 978-1-7185-0340-3 (print) ISBN-13: 978-1-7185-0341-0 (ebook) ® Published by No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Allison Felus Developmental Editor: Frances Saux Cover Illustrator: Josh Ellingson Interior Design: Octopod Studios Technical Reviewer: Daniel Zingaro Copyeditor: Audrey Doyle Proofreader: Daniel Wolff The Library of Congress Control Number for the first edition is 2014953114. For permissions beyond the scope of this license or customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, or corporate sales: sales@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. No Starch Press and the No Starch Press iron logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
For my nephew Jack
(This page has no text content)
About the Author Al Sweigart is a software developer, author, artist, and fellow of the Python Software Foundation. He is the author of several programming books for beginners, including Invent Your Own Computer Games with Python, The Big Book of Small Python Projects, and Beyond the Basic Stuff with Python (all from No Starch Press). He is an international speaker at several PyCon confer- ences. His website is https://inventwithpython .com. Reports that Al is an AI have been grossly exaggerated. About the Technical Reviewer Dr. Daniel Zingaro is an associate professor of computer science at the University of Toronto. He is internationally known for his uniquely interac- tive approach to teaching, his leading research on teaching with generative AI, and his learner-centered textbooks, which are used by thousands of students around the world. He is the author of Algorithmic Thinking, 2nd edition (No Starch Press, 2024) and Learn to Code by Solving Problems (No Starch Press, 2021) and co-author of Learn AI-Assisted Python Programming with GitHub Copilot and ChatGPT (Manning, 2023).
(This page has no text content)
B R I E F C O N T E N T S Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi PART I: PROGRAMMING FUNDAMENTALS . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1: Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: if-else and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Chapter 3: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chapter 4: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Chapter 5: Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Chapter 6: Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Chapter 7: Dictionaries and Structuring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Chapter 8: Strings and Text Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 PART II: AUTOMATING TASKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 Chapter 9: Text Pattern Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . 185 Chapter 10: Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Chapter 11: Organizing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Chapter 12: Designing and Deploying Command Line Programs . . . . . . . . . . . . . . . . . . . 257 Chapter 13: Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Chapter 14: Excel Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Chapter 15: Google Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Chapter 16: SQLite Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 Chapter 17: PDF and Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
x   Brief Contents Chapter 18: CSV, JSON, and XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Chapter 19: Keeping Time, Scheduling Tasks, and Launching Programs . . . . . . . . . . . . . . 459 Chapter 20: Sending Email, Texts, and Push Notifications . . . . . . . . . . . . . . . . . . . . . . . . 479 Chapter 21: Making Graphs and Manipulating Images . . . . . . . . . . . . . . . . . . . . . . . . . 493 Chapter 22: Recognizing Text in Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 Chapter 23: Controlling the Keyboard and Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 Chapter 24: Text-to-Speech and Speech Recognition Engines . . . . . . . . . . . . . . . . . . . . . . 565 Appendix A: Installing Third-Party Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 Appendix B: Answers to the Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
C O N T E N T S I N D E T A I L FOREWORD xxvii ACKNOWLEDGMENTS xxix INTRODUCTION xxxi Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxii Coding Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxii What Is Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii What Is Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv Common Myths About Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv Programmers Don’t Need to Know Much Math . . . . . . . . . . . . . . . . . . . . . . xxxiv You Are Not Too Old to Learn Programming . . . . . . . . . . . . . . . . . . . . . . . . xxxv AI Won’t Replace Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvi About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii Downloading and Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix Downloading and Installing Mu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix Starting Mu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl Starting IDLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl The Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl How to Find Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xli Asking Smart Programming Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xlii New to the Third Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xliii Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xliv PART I: PROGRAMMING FUNDAMENTALS 1 1 PYTHON BASICS 3 Entering Expressions into the Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The Integer, Floating-Point, and String Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 String Concatenation and Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Storing Values in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Dissecting the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The print() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 The input() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Greeting Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The len() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 The str(), int(), and float() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
xii   Contents in Detail The type() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 The round() and abs() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 How Computers Store Data with Binary Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2 IF-ELSE AND FLOW CONTROL 27 Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Mixing Boolean and Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Components of Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Program Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Flow Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 A Short Program: Opposite Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 A Short Program: Dishonest Capacity Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3 LOOPS 49 while Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 An Annoying while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 break Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 for Loops and the range() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 An Equivalent while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Arguments to range() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Ending a Program Early with sys .exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 A Short Program: Guess the Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 A Short Program: Rock, Paper, Scissors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4 FUNCTIONS 73 Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Return Values and return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The None Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 The Call Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Contents in Detail   xiii Local and Global Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Scope Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 The global Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Scope Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 A Short Program: Zigzag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 A Short Program: Spike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 The Collatz Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5 DEBUGGING 95 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 The logging Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Logfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 A Poor Practice: Debugging with print() . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Logging Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Disabled Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Mu’s Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Debugging an Addition Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Setting Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Practice Program: Debugging Coin Toss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 6 LISTS 109 The List Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Negative Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 The len() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Value Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Concatenation and Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 del Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 for Loops and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 The in and not in Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 The Multiple Assignment Trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 List Item Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Random Selection and Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Augmented Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Finding Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Adding Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Removing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
xiv   Contents in Detail Sorting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Reversing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Short-Circuiting Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 A Short Program: Magic 8 Ball with a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Sequence Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Mutable and Immutable Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 The Tuple Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 List and Tuple Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 The copy() and deepcopy() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 A Short Program: The Matrix Screensaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Comma Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Coin Flip Streaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 7 DICTIONARIES AND STRUCTURING DATA 139 The Dictionary Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Comparing Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Returning Keys and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Checking Whether a Key Exists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Setting Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Model Real-World Things Using Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Project 1: Interactive Chessboard Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Step 1: Set Up the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Step 2: Create a Chessboard Template . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Step 3: Print the Current Chessboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Step 4: Manipulate the Chessboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Nested Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Chess Dictionary Validator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Fantasy Game Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 List-to-Dictionary Loot Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 8 STRINGS AND TEXT EDITING 159 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Indexes and Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 The in and not in Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 F-Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 F-String Alternatives: %s and format() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Useful String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Changing the Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Checking String Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Contents in Detail   xv Checking the Start or End of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Joining and Splitting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Justifying and Centering Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Removing Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Numeric Code Points of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Copying and Pasting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Project 2: Add Bullets to Wiki Markup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 Step 1: Copy and Paste from the Clipboard . . . . . . . . . . . . . . . . . . . . . . . . 174 Step 2: Separate the Lines of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Step 3: Join the Modified Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 A Short Program: Pig Latin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Practice Program: Table Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 PART II: AUTOMATING TASKS 183 9 TEXT PATTERN MATCHING WITH REGULAR EXPRESSIONS 185 Finding Text Patterns Without Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Finding Text Patterns with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 The Syntax of Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Grouping with Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Using Escape Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Matching Characters from Alternate Groups . . . . . . . . . . . . . . . . . . . . . . . . 191 Returning All Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Qualifier Syntax: What Characters to Match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Using Character Classes and Negative Character Classes . . . . . . . . . . . . . . 193 Using Shorthand Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Matching Everything with the Dot Character . . . . . . . . . . . . . . . . . . . . . . . . 194 Being Careful What You Match For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Quantifier Syntax: How Many Qualifiers to Match . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Matching an Optional Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Matching Zero or More Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Matching One or More Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Matching a Specific Number of Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . 198 Greedy and Non-greedy Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Matching Everything . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Matching Newline Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Matching at the Start and End of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Case-Insensitive Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Substituting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Managing Complex Regexes with Verbose Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Combining re .IGNORECASE, re .DOTALL, and re .VERBOSE . . . . . . . . . . . . . . . . . . . 205 Project 3: Extract Contact Information from Large Documents . . . . . . . . . . . . . . . . . . . . .205 Step 1: Create a Regex for Phone Numbers . . . . . . . . . . . . . . . . . . . . . . . . 206 Step 2: Create a Regex for Email Addresses . . . . . . . . . . . . . . . . . . . . . . . . 207
xvi   Contents in Detail Step 3: Find All Matches in the Clipboard Text . . . . . . . . . . . . . . . . . . . . . . 207 Step 4: Join the Matches into a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Humre: A Module for Human-Readable Regexes . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Strong Password Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Regex Version of the strip() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 10 READING AND WRITING FILES 217 Files and Filepaths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Standardizing Path Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Joining Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Accessing the Current Working Directory . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Accessing the Home Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Specifying Absolute vs . Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Creating New Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Handling Absolute and Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Getting the Parts of a Filepath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Finding File Sizes and Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Finding Files Using Glob Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Checking Path Validity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 The File Reading and Writing Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Reading the Contents of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Using with Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Saving Variables with the shelve Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Project 4: Generate Random Quiz Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 Step 1: Store the Quiz Data in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . 236 Step 2: Create the Quiz File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Step 3: Create the Answer Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Step 4: Write the Content to the Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Mad Libs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Regex Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 11 ORGANIZING FILES 243 The shutil Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Copying Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Moving and Renaming Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Permanently Deleting Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Deleting to the Recycle Bin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Walking a Directory Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Contents in Detail   xvii Compressing Files with the zipfile Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Creating and Adding to ZIP Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Reading ZIP Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Extracting from ZIP Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Project 5: Back Up a Folder into a ZIP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252 Step 1: Figure Out the ZIP File’s Name . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Step 2: Create the New ZIP File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Step 3: Walk the Directory Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Ideas for Other Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Practice Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Selectively Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Deleting Unneeded Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Renumbering Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Converting Dates from American- to European-Style . . . . . . . . . . . . . . . . . . 256 12 DESIGNING AND DEPLOYING COMMAND LINE PROGRAMS 257 A Program by Any Other Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Using the Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 The cd, pwd, dir, and ls Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 The PATH Environment Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 PATH Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The which and where Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Virtual Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Installing Python Packages with pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Self-Aware Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Text-Based Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Short Command Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Clipboard I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Colorful Text with Bext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Terminal Clearing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Sound and Text Notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 A Short Program: Snowstorm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Pop-up Message Boxes with PyMsgBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Deploying Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 A Short Program: Copying the Current Working Directory . . . . . . . . . . . . . . . . . . . . . 279 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 A Short Program: Clipboard Recorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
xviii   Contents in Detail Compiling Python Programs with PyInstaller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Practice Program: Make Your Programs Deployable . . . . . . . . . . . . . . . . . . . . . . . . . 287 13 WEB SCRAPING 289 HTTP and HTTPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Project 6: Run a Program with the webbrowser Module . . . . . . . . . . . . . . . . . . . . . . . . .291 Step 1: Figure Out the URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Step 2: Handle the Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . 292 Step 3: Retrieve the Clipboard Content . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Downloading Files from the Web with the requests Module . . . . . . . . . . . . . . . . . . . . 294 Downloading Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Checking for Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Saving Downloaded Files to the Hard Drive . . . . . . . . . . . . . . . . . . . . . . . . 296 Accessing a Weather API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Requesting a Latitude and Longitude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Fetching the Current Weather . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Getting a Weather Forecast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Exploring APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Understanding HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Exploring the Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Viewing a Web Page’s Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Opening Your Browser’s Developer Tools . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Finding HTML Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Parsing HTML with Beautiful Soup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Creating a Beautiful Soup Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Finding an Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Getting Data from an Element’s Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Project 7: Open All Search Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310 Step 1: Get the Search Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Step 2: Find All Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Step 3: Open Web Browsers for Each Result . . . . . . . . . . . . . . . . . . . . . . . 311 Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Project 8: Download XKCD Comics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312 Step 1: Design the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Step 2: Download the Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Step 3: Find and Download the Comic Image . . . . . . . . . . . . . . . . . . . . . . 315 Step 4: Save the Image and Find the Previous Comic . . . . . . . . . . . . . . . . . 316 Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Controlling the Browser with Selenium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Starting a Selenium-Controlled Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Clicking Browser Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Finding Elements on the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Clicking Elements on the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Filling Out and Submitting Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Sending Special Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322