Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Python Programming, 3rd edition

You're reading from   Learn Python Programming, 3rd edition An in-depth introduction to the fundamentals of Python

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801815093
Length 554 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Heinrich Kruger Heinrich Kruger
Author Profile Icon Heinrich Kruger
Heinrich Kruger
Fabrizio Romano Fabrizio Romano
Author Profile Icon Fabrizio Romano
Fabrizio Romano
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. A Gentle Introduction to Python 2. Built-In Data Types FREE CHAPTER 3. Conditionals and Iteration 4. Functions, the Building Blocks of Code 5. Comprehensions and Generators 6. OOP, Decorators, and Iterators 7. Exceptions and Context Managers 8. Files and Data Persistence 9. Cryptography and Tokens 10. Testing 11. Debugging and Profiling 12. GUIs and Scripting 13. Data Science in Brief 14. Introduction to API Development 15. Packaging Python Applications 16. Other Books You May Enjoy
17. Index

First approach: scripting

Now, let's start writing the script. We'll go through the source in three steps: imports, argument parsing, and business logic.

The imports

Here's how the script starts:

# scrape.py
import argparse
import base64
import json
from pathlib import Path
from bs4 import BeautifulSoup
import requests

Going through the imports from the top, you can see that we'll need to parse the arguments, which we'll feed to the script itself (using argparse). We will need the base64 library to save the images within a JSON file (so we will also need json), and we'll need to open files for writing (using pathlib). Finally, we'll need BeautifulSoup for scraping the web page easily, and requests to fetch its content. We assume you're familiar with requests as we used it in Chapter 8, Files and Data Persistence.

We will explore the HTTP protocol and the requests mechanism in Chapter 14, Introduction to API Development...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime