Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 17: Mastering Requests, Regex, and Beautiful Soup
Transcript
- 0:00Speaker 1: Imagine for a second that you are staring at a
- 0:03Speaker 1: web page, okay, and it is just packed with thousands
- 0:07Speaker 1: of data points. Like we are talking, pages upon pages
- 0:10Speaker 1: of you know, randomly generated addresses, phone numbers, product reviews, whatever.
- 0:15Speaker 2: Oh yeah, I know that feeling.
- 0:16Speaker 1: Right, You get that sinking feeling in your stomach, that
- 0:19Speaker 1: creeping dread when you realize you need all of that
- 0:21Speaker 1: data neatly organized in a spreadsheet and the only way
- 0:25Speaker 1: you know how to do it is by uh spending
- 0:27Speaker 1: the next four maybe five hours mindlessly highlighting, copying, and pasting.
- 0:33Speaker 2: Yeah, it's the ultimate digital bottleneck. I mean, your brain
- 0:36Speaker 2: is totally ready to analyze the information and find patterns,
- 0:40Speaker 2: but your hands are just stuck doing this manual, repetitive
- 0:44Speaker 2: labor from like the nineteen nineties exactly.
- 0:46Speaker 1: It completely breaks your momentum. But you know what if
- 0:49Speaker 1: you didn't have to do that, What if you could
- 0:50Speaker 1: write just a few lines of code and instantly download, extract,
- 0:53Speaker 1: and organize every single piece of that data in a
- 0:56Speaker 1: matter of seconds. So today we're taking a deep dive
- 1:00Speaker 1: into the superpower of webscraping and data extraction using Python.
- 1:04Speaker 2: It really is a superpower.
- 1:05Speaker 1: Honestly, it really is, and we're going to map out
- 1:08Speaker 1: a very specific journey for you today. First, we'll figure
- 1:12Speaker 1: out how to get a web page to actually talk
- 1:15Speaker 1: to our Python environment without blocking us. Second, we're going
- 1:19Speaker 1: to explore how to search through all that chaotic raw
- 1:22Speaker 1: code using some incredibly powerful pattern matching tools. And finally,
- 1:27Speaker 1: we will look at how to elegantly carve up the
- 1:30Speaker 1: structural matrix of the web so you can pull out
- 1:32Speaker 1: exactly what you need.
- 1:34Speaker 2: It's a massive shift in how you interact with the Internet.
- 1:36Speaker 2: You basically move from being a passive consumer manually browsing
- 1:40Speaker 2: pages to becoming an active curator who automates the ingestion
- 1:44Speaker 2: of knowledge.
- 1:45Speaker 1: I love that active curator. So let's start at the
- 1:48Speaker 1: very beginning. Before we can parse any data, we actually
- 1:50Speaker 1: have to get the raw web page into our Python environment.
- 1:53Speaker 1: I always picture this as like sending a highly efficient
- 1:56Speaker 1: robot butler to a store to fetch a specific document
- 1:59Speaker 1: for you. Great analogy, thanks, But how do we actually
- 2:03Speaker 1: build that robot.
- 2:04Speaker 2: Well, in the language of the web, setting that robot
- 2:07Speaker 2: butler is called making an HTTP get request. You're pointing
- 2:11Speaker 2: your code at a target URL and asking or getting
- 2:15Speaker 2: the information from that server, right, and within the Python ecosystem,
- 2:19Speaker 2: there are really three primary libraries you'll encounter for this job.
- 2:23Speaker 1: Let's walk through those because I know they each have
- 2:25Speaker 1: a very different philosophy.
- 2:26Speaker 2: They definitely do. So first you have httplib two. This
- 2:31Speaker 2: is actually a really comprehensive, powerful upgrade that was originally
- 2:34Speaker 2: driven by developers at Google.
- 2:36Speaker 1: Oh wow, okay, yeah, and it.
- 2:37Speaker 2: Gives you very fine grained, granular control over your requests.
- 2:41Speaker 2: But you know that power comes with a cost. It
- 2:44Speaker 2: requires you to manually manage a lot of the underlying connection.
- 2:47Speaker 1: Details, which sounds like a headache for a beginner.
- 2:49Speaker 2: It can be. Then you have earlib, which.
- 2:52Speaker 1: I understand is incredibly common just because it comes built
- 2:54Speaker 1: in with standard Python right like, you don't have to
- 2:57Speaker 1: install a third party package to use it exactly.
- 2:59Speaker 2: That is its main appeal. Historically, older versions of Python
- 3:04Speaker 2: actually had two separate, somewhat overlapping libraries, arlib and earlib two.
- 3:09Speaker 1: Oh that sounds confusing.
- 3:11Speaker 2: Its big confusing for developers, so they eventually cleaned house
- 3:14Speaker 2: and merge them into a single unified arlib module. It
- 3:18Speaker 2: sits at a higher level than httplib two, so it
- 3:21Speaker 2: handles some of those low level connection headaches for you, okay,
- 3:24Speaker 2: that's better, But honestly, for most modern developers, the undisputed
- 3:29Speaker 2: favorite is a library simply called Requests just requests.
- 3:33Speaker 1: The name itself kind of tells you it's trying to
- 3:35Speaker 1: be user friendly.
- 3:36Speaker 2: It is beautifully designed. The Request library provides a high level,
- 3:41Speaker 2: incredibly intuitive interface. It abstracts away all the tedious, complex
- 3:46Speaker 2: connection logic.
- 3:47Speaker 1: Nice.
- 3:47Speaker 2: If your primary goal is just to grab data quickly
- 3:50Speaker 2: and cleanly without fighting with connection protocols, Requests is the
- 3:54Speaker 2: tool you reach for.
- 3:55Speaker 1: Okay, So we pick our library, say requests, and we
- 3:57Speaker 1: send our automated requests out to a website. When the
- 4:00Speaker 1: server responds, it doesn't just hand over a clean, readable
- 4:03Speaker 1: text document. The robot comes back with a response object.
- 4:07Speaker 1: What is actually inside that package?
- 4:09Speaker 2: So the response object fundamentally breaks down into two parts,
- 4:13Speaker 2: the headers and the content. The headers are basically the
- 4:16Speaker 2: metadata of the transaction.
- 4:18Speaker 1: Like a shipping label.
- 4:19Speaker 2: Exactly like a shipping label, they tell you the status code,
- 4:22Speaker 2: whether the server actually sound the page or if it crashed,
- 4:25Speaker 2: what kind of server software is running, and various caching rules.
- 4:29Speaker 1: I noticed something interesting in the notes about those headers
- 4:32Speaker 1: regarding security. If you accidentally point your code to an old,
- 4:36Speaker 1: unsecure HTTP address, the server often fires back an HTTP
- 4:41Speaker 1: three oh one redirect status.
- 4:43Speaker 2: Yes, that happens a lot.
- 4:44Speaker 1: It basically intercepts your robot and automatically reroutes it to
- 4:47Speaker 1: the secure HTTPS version of the site.
- 4:50Speaker 2: Modern servers are heavily configured to prioritize encrypted traffic. They'll
- 4:54Speaker 2: seamlessly bounce your automated request to the secure lane, just
- 4:57Speaker 2: like they would for a human typing an address into
- 4:59Speaker 2: a browser.
- 5:00Speaker 1: That makes sense.
- 5:01Speaker 2: Now, underneath those metadata headers is the actual content you
- 5:04Speaker 2: ask for. But as you hinted earlier, it does not
- 5:07Speaker 2: arrive as readable text. It arises raw bytes machine language.
- 5:11Speaker 1: Which looks like absolute garbage to a human eye. If
- 5:13Speaker 1: you try to print it.
- 5:14Speaker 2: Out total garbage. To make it readable, you have to
- 5:17Speaker 2: decode that byte stream. The vast majority of the web
- 5:20Speaker 2: is encoded using a standard called UTF eight. Okay, so
- 5:23Speaker 2: within your Python script, you take those raw bytes, apply
- 5:26Speaker 2: a UTF eight decoding method, and suddenly that machine code
- 5:30Speaker 2: blossoms into the giant, messy HTML string that actually builds
- 5:34Speaker 2: the web page.
- 5:34Speaker 1: I want to pause here for a second, because there
- 5:36Speaker 1: is a glaring real world problem with this robot butler analogy.
- 5:41Speaker 2: Oh what's that?
- 5:42Speaker 1: Well, what if the store doesn't like robots? Like if
- 5:45Speaker 1: I write a script right now and send it to
- 5:47Speaker 1: scrape search results from Google, Google isn't just going to
- 5:50Speaker 1: hand over it to data with a smile.
- 5:51Speaker 2: Right, Oh? Absolutely not. If you try that using the
- 5:54Speaker 2: default settings of erlib or requests, you will immediately hit
- 5:57Speaker 2: a brick wall. Specifically, the server will turn an HTTP
- 6:01Speaker 2: air four h three forbidden.
- 6:02Speaker 1: Yikes, forbidden.
- 6:03Speaker 2: Yeah. Major tech companies actively monitor incoming traffic. When they
- 6:07Speaker 2: see a request, they check the headers, and by default,
- 6:10Speaker 2: your Python library includes a header called the user agent
- 6:13Speaker 2: and essentially wears a giant name pag that says hello,
- 6:16Speaker 2: I'm an automated Python script.
- 6:18Speaker 1: And companies block that because they don't want armies of
- 6:21Speaker 1: bots chewing up their server bandwidth or mass downloading their
- 6:25Speaker 1: proprietary data.
- 6:26Speaker 2: Exactly.
- 6:27Speaker 1: So, how do we get past the bouncer?
- 6:29Speaker 2: You have to change your outfit. Yeah, you modify the
- 6:31Speaker 2: user agent header in your code before you send the request.
- 6:34Speaker 2: Sneaky very. Instead of broadcasting that you're a Python script,
- 6:39Speaker 2: you spoof the identity of a standard web browser. You
- 6:42Speaker 2: pass a string that tells the server, Hey, I'm not
- 6:44Speaker 2: a bot. I'm just a normal user running the Safari
- 6:47Speaker 2: browser on a MacBook.
- 6:48Speaker 1: And that actually works.
- 6:49Speaker 2: It does. Once the server reads that fabricated header, it
- 6:53Speaker 2: drops the velvet rope and lets you in. Wow.
- 6:55Speaker 1: Okay, so we spoof our identity, the bouncer lets us through,
- 6:59Speaker 1: and our script downloads the page. Now we're staring at
- 7:02Speaker 1: a massive, thousands of lines long wall of raw HTML text.
- 7:07Speaker 1: Standard searching methods like hitting ctrlf on your keyboard to
- 7:10Speaker 1: find a word are completely useless here.
- 7:13Speaker 2: Yeah, completely.
- 7:13Speaker 1: If we want to extract thousands of dynamic data points,
- 7:16Speaker 1: we need something much more robust.
- 7:18Speaker 2: We need regular expressions, or as it's often abbreviated, redjax.
- 7:23Speaker 2: You access this in Python through the built in a
- 7:26Speaker 2: EARS module. Rojex isn't just a search tool. It is
- 7:30Speaker 2: an entire micro language dedicated to advanced pattern matching within
- 7:35Speaker 2: strings of text.
- 7:36Speaker 1: I want to break down how we actually deploy this,
- 7:38Speaker 1: because there are several different functions inside that module depending
- 7:41Speaker 1: on your strategy. Sure, let's look at the match function first.
- 7:44Speaker 1: I picture match as like walking into a train station
- 7:49Speaker 1: to see if a specific train is sitting exactly the
- 7:51Speaker 1: very first platform the moment you.
- 7:53Speaker 2: Arrive that analogy holds up perfectly. The match function is
- 7:57Speaker 2: incredibly strict. It only checks for your pattern at the
- 8:00Speaker 2: absolute literal beginning of a string position zero. If the
- 8:05Speaker 2: data you want is buried even one space deep into
- 8:08Speaker 2: the text, match will return absolutely nothing.
- 8:10Speaker 1: Wow. Okay.
- 8:11Speaker 2: There's also full match, which goes a step further. It
- 8:14Speaker 2: demands that the entire string, from the very first character
- 8:17Speaker 2: to the absolute last matches your pattern perfectly.
- 8:20Speaker 1: But in the context of web scraping, I rarely care
- 8:23Speaker 1: if the data is sitting perfectly at the start of
- 8:25Speaker 1: the document. I just want to find it wherever it's
- 8:27Speaker 1: hiding in the chaos. You know. So, if match is
- 8:30Speaker 1: checking the starting platform, the search function is like flying
- 8:34Speaker 1: a helicopter down the entire length of the train tracks.
- 8:37Speaker 2: Yes, the search function scans through the entire string and
- 8:41Speaker 2: stops the exact moment it finds the very first instance
- 8:44Speaker 2: of your pattern. It finds that first train reports its
- 8:48Speaker 2: location and stops looking.
- 8:50Speaker 1: Stops looking entirely entirely.
- 8:52Speaker 2: But in data extraction, stopping at the first hit kind
- 8:55Speaker 2: of defeats the purpose. If you are scraping a directory
- 8:59Speaker 2: for phone numbers, you want every single number on.
- 9:01Speaker 1: The page, right, not just the first one.
- 9:03Speaker 2: For that, you use findal.
- 9:05Speaker 1: Okay, find ol does exactly what it says on the ten.
- 9:08Speaker 1: It sweeps the entire document and hands you back a clean,
- 9:11Speaker 1: comprehensive list of every single matchup.
- 9:13Speaker 2: Fat It's the most common one you'll use, for sure.
- 9:16Speaker 1: I also saw a function called find it or, which
- 9:18Speaker 1: sounds like it's doing something similar but with more complexity.
- 9:21Speaker 2: So find it or is the tool you use when
- 9:22Speaker 2: you need spatial awareness. Findal just gives you the extracted words,
- 9:26Speaker 2: but find it or returns an iterator that provides the
- 9:29Speaker 2: exact character index ranges for every match. Oh interesting, Yeah,
- 9:32Speaker 2: it tells you I found your target data and it
- 9:35Speaker 2: exists precisely from character position one fifty to one sixty two.
- 9:39Speaker 2: It acts like a GPS coordinate system for your data.
- 9:42Speaker 1: That is super useful, and beyond just locating data, you
- 9:46Speaker 1: can manipulate it too. You can use the split function
- 9:49Speaker 1: to chop a massive paragraph into individual tokens based on spaces,
- 9:53Speaker 1: or the subfunction to rapidly substitute or replace specific patterns
- 9:57Speaker 1: across the entire document.
- 9:59Speaker 2: Absolutely, so, here's the real hurdle.
- 10:01Speaker 1: To truly utilize regular expressions. We have to stop searching
- 10:04Speaker 1: for literal words like apple or banana. We need to
- 10:08Speaker 1: search for abstract concepts. We need to find any decimal
- 10:11Speaker 1: number or any capitalized word.
- 10:13Speaker 2: And this is where we introduce special sequences. But a
- 10:16Speaker 2: quick best practice note first, before you write these sequences,
- 10:19Speaker 2: you should prefix your pattern string with a lowercase R.
- 10:22Speaker 2: This creates what's called a raw string.
- 10:24Speaker 1: Wait, why is that necessary?
- 10:26Speaker 2: Well, by default, Python uses backslashes as escape characters for
- 10:31Speaker 2: things like creating new lines. Because Rejects relies heavily on
- 10:35Speaker 2: backslashes for its own syntax, Using a raw string tells
- 10:38Speaker 2: Python to ignore its default rules and treat every backslash
- 10:42Speaker 2: as a literal character for the Rejects engine to interpret.
- 10:45Speaker 2: Oh I see, yeah, it saves you from writing incredibly
- 10:47Speaker 2: messy tangled code. Additionally, you will often wrap your raw
- 10:52Speaker 2: string in the redot compile method. This translates your pattern
- 10:56Speaker 2: into a highly efficient, reusable blueprint, which fads things up
- 11:00Speaker 2: dramatically if you're applying it thousands of times.
- 11:02Speaker 1: Okay, let's talk about these special sequences because they are
- 11:05Speaker 1: the building blocks of that blueprint. But I'll be honest.
- 11:07Speaker 1: When you look at them on paper, they look like
- 11:09Speaker 1: a secret code.
- 11:10Speaker 2: They do require a slight mental chef. Let's look at
- 11:13Speaker 2: those common ones. A backslash followed by a lowercase D.
- 11:16Speaker 2: So backslash D represents any single digit zero through nine.
- 11:20Speaker 1: Got it, And if you capitalize the letter, it flips
- 11:22Speaker 1: the meaning. Right. Yeah, So a backslash and an uppercase
- 11:25Speaker 1: D matches any character that is not a digit exactly.
- 11:29Speaker 2: That capitalization rule applies across the board. A backslash lowercase
- 11:34Speaker 2: S hunts for white space, spaces, tabs, line breaks, A
- 11:38Speaker 2: backslash uppercase S finds anything that is into space.
- 11:42Speaker 1: Okay, tracking so far.
- 11:43Speaker 2: Then there's backslash lowercase W, which is your workhourse for
- 11:47Speaker 2: alphanumeric characters. It matches letters, numbers, and underscores. Capitalize it
- 11:52Speaker 2: to backslash W, and it matches non alphanumeric characters like
- 11:56Speaker 2: punctuation marks.
- 11:57Speaker 1: You can also define your own custom ranges using square back.
- 12:00Speaker 1: That's right. Like if I only want to find numbers
- 12:01Speaker 1: between five and nine, I just write bracket five dash nine,
- 12:04Speaker 1: pracket yeap. It tells the engine only match characters that
- 12:07Speaker 1: fall inside this specific parameter.
- 12:09Speaker 2: You've got it. You also need a way to tell
- 12:12Speaker 2: the engine how many times a character should occur. We
- 12:15Speaker 2: use multipliers for this. An asterisk means the preceding character
- 12:19Speaker 2: can appear zero or more times. A plus sign means
- 12:22Speaker 2: it must appear one or more times.
- 12:24Speaker 1: Okay, I understand the individual puzzle pieces, but when you
- 12:26Speaker 1: string them together, the syntax becomes incredibly intimidating.
- 12:30Speaker 2: It can look a bit overwhelming at first glance.
- 12:32Speaker 1: Let's look at a real world example from a script.
- 12:35Speaker 1: I see a pattern that reads backslash D curly brace
- 12:38Speaker 1: three dash, backslash d curly brace three dash backslash d
- 12:42Speaker 1: curly brace four. That looks like someone dropped a coffee
- 12:45Speaker 1: mug on their keyboard. How do you read that fluently?
- 12:48Speaker 2: It looks chaotic until you apply the rules we just discussed.
- 12:51Speaker 2: Let's translate it piece by piece. The backslash d means
- 12:55Speaker 2: we're looking for a digit. The curly braces the three
- 12:58Speaker 2: inside specify an exact multiplier. We don't want zero or more,
- 13:02Speaker 2: we want exactly three. So backslash d curly brace three
- 13:06Speaker 2: translates to find exactly three numbers in a row.
- 13:08Speaker 1: Okay, Then there is a literal hyphen.
- 13:11Speaker 2: Then another backslash curly brace three, so three more numbers,
- 13:13Speaker 2: another hyphen and finally backslash curly brase four meaning exactly four.
- 13:17Speaker 1: Numbers wait three digits, dash three digits, dash four digits.
- 13:21Speaker 1: When you look at it that way, the gibberish becomes
- 13:24Speaker 1: a surgically precise blueprint for a standard US phone number exactly.
- 13:28Speaker 2: You have just created a custom extraction tool. You can
- 13:31Speaker 2: pass that exact tiny string of characters into a final function,
- 13:35Speaker 2: unleash it on a ten thousand line HTML document, and
- 13:39Speaker 2: it will effortlessly sift through the chaos and hand you
- 13:42Speaker 2: a perfect list of every single phone number on the page.
- 13:45Speaker 1: That is wild. Okay, let's take this new found power
- 13:48Speaker 1: and apply it to the HTML we fetched earlier. Web
- 13:51Speaker 1: pages are built using tags, so the title of a
- 13:54Speaker 1: page is wrapped in title tags.
- 13:56Speaker 2: Yes, and if you want to extract that title using rejects,
- 14:00Speaker 2: your first instinct might be to write a pattern like
- 14:02Speaker 2: title tag, dot asterisk closing title tag. The dot represents
- 14:06Speaker 2: any character, and the asterisk means repeated zero or more times. Right,
- 14:10Speaker 2: it will grab the opening tag, everything in the middle,
- 14:13Speaker 2: and the closing tag.
- 14:13Speaker 1: But the problem with that is you don't actually want
- 14:15Speaker 1: the literal HTML tags in your final spreadsheet, you only
- 14:18Speaker 1: want the human readable text sandwiched between them spot on.
- 14:23Speaker 2: To isolate that text, you introduce capturing groups, which are
- 14:26Speaker 2: created using parentheses. You update your pattern to put parentheses
- 14:29Speaker 2: around the dot asterisk A. The rejex engine sees those
- 14:32Speaker 2: parentheses and understands the instruction. I need you to locate
- 14:36Speaker 2: this entire structure to ensure we are in the right place,
- 14:39Speaker 2: but I only want you to extract and return the
- 14:42Speaker 2: specific data trapped inside the parentheses.
- 14:45Speaker 1: That's brilliant. Let's scale that up. Imagine we are scraping
- 14:49Speaker 1: a directory of employees. The web page lists them using
- 14:52Speaker 1: HTML bullet points, which are designated by lie tags. We
- 14:56Speaker 1: want to extract the names of everyone whose first name
- 14:58Speaker 1: starts with the letter C.
- 15:00Speaker 2: Construct a pattern that looks for the lie tag followed
- 15:03Speaker 2: by a capital C followed by a backslash W plus
- 15:06Speaker 2: to capture the rest of the alphanumeric characters in the name.
- 15:10Speaker 2: You wrap that entire name segment in parentheses, and the
- 15:12Speaker 2: engine will spit out a clean list of first names.
- 15:15Speaker 1: And you can even set up multiple capturing groups in
- 15:17Speaker 1: a single pattern to extract complex data sets. Right like,
- 15:21Speaker 1: you could create one group for a city, another group
- 15:23Speaker 1: for the two letter state code, and a final group
- 15:25Speaker 1: for the five digit zip code.
- 15:27Speaker 2: Exactly. The rejects engine will return those matches as neatly
- 15:30Speaker 2: organized data tipples. You are pulling highly structured, relational data
- 15:34Speaker 2: straight out of raw code.
- 15:36Speaker 1: But as we walk through these HTML examples, a massive
- 15:39Speaker 1: vulnerability becomes obvious to me. Look that HTML is notoriously messy.
- 15:44Speaker 1: Web developers are human. They make mistakes, they update site layouts,
- 15:48Speaker 1: they do weird things. What if a developer decides a
- 15:52Speaker 1: specific employee's name needs to stand out, so they inject
- 15:54Speaker 1: an HTML bold tag right in the middle of the
- 15:57Speaker 1: first name. Oh yeah, Or what if they accidentally leave
- 15:59Speaker 1: a line break where there shouldn't be one. Rajex doesn't
- 16:03Speaker 1: actually understand HTML. It just blindly reads raw characters from
- 16:07Speaker 1: left to right.
- 16:08Speaker 2: That is the fundamental limitation of using regular expressions as
- 16:12Speaker 2: your primary webscribing tool. It completely lacks structural awareness.
- 16:16Speaker 1: It feels like trying to navigate a sprawling, multi level
- 16:20Speaker 1: city while totally blindfolded, relying exclusively on a compass like
- 16:24Speaker 1: the compass might tell you that you are heading directly
- 16:27Speaker 1: north toward your target data, but it has no idea
- 16:29Speaker 1: that there is a brick wall in front of you,
- 16:31Speaker 1: or that the data is actually on the third floor
- 16:33Speaker 1: of a building.
- 16:34Speaker 2: The compass analogy is perfect. Rejects is rigid and literal.
- 16:38Speaker 2: If the underlying structure of the HTML shifts even slightly,
- 16:41Speaker 2: your carefully crafted rejects pattern instantly shatters. Right. If rejects
- 16:46Speaker 2: is a blind compass, what we actually need is a detailed,
- 16:48Speaker 2: three dimensional map of the city. We need a tool
- 16:51Speaker 2: that understands the architecture, and in Python that map is
- 16:54Speaker 2: a library called Beautiful Soup.
- 16:55Speaker 1: Beautiful Soup or BS four for short, HUG. I want
- 16:58Speaker 1: to understand how it actually creates this map, because the
- 17:02Speaker 1: HTML we feed it is still just a chaotic string
- 17:05Speaker 1: of text.
- 17:05Speaker 2: Well, beautiful Soup does not work alone. It relies on
- 17:08Speaker 2: underlying parsing engines, most commonly LexML or HTML five lib.
- 17:12Speaker 2: Think of these parsers as the heavy machinery. Okay, they
- 17:15Speaker 2: ingest the messy, broken HTML string, figure out how all
- 17:19Speaker 2: the open and closed tags relate to each other, and
- 17:21Speaker 2: transform it into a highly structured part tree that Python
- 17:24Speaker 2: can interact with. Beautiful Soup is just the elegant interface
- 17:27Speaker 2: that lets you navigate that tree, and.
- 17:29Speaker 1: One of the first commands you run when you load
- 17:31Speaker 1: that tree is soup dot pritiphy. It takes that unreadable
- 17:35Speaker 1: block of code and magically applies proper indentation. It visually
- 17:39Speaker 1: structures the HTML so a human can instantly see which
- 17:42Speaker 1: tags are nested inside of other tags. It restores your sanity,
- 17:46Speaker 1: it really does.
- 17:48Speaker 2: But the true magic is in the navigation using what
- 17:50Speaker 2: is known as dot notation.
- 17:52Speaker 1: Right with rejex, if we wanted the page title, we
- 17:55Speaker 1: had to build wild cards and capturing groups to sift
- 17:58Speaker 1: through characters. With beautiful soup, you don't use wild cards.
- 18:01Speaker 1: You just type soup dot title.
- 18:03Speaker 2: Dot string exactly. You literally ask the soup object for
- 18:06Speaker 2: the title element and then ask for the string inside it,
- 18:09Speaker 2: and it just hands it to you. If you want
- 18:10Speaker 2: the very first paragraph inside the body of the web page,
- 18:13Speaker 2: you don't write a pattern. You just type soup dot
- 18:15Speaker 2: body dot p It elegantly walks down the branches of
- 18:18Speaker 2: the parch.
- 18:18Speaker 1: Tree that is so clean. It understands the concept of
- 18:21Speaker 1: a hierarchical family tree. It knows what a parent element is,
- 18:25Speaker 1: what a child element is, and what a sibling element is.
- 18:28Speaker 2: Every single navigable piece of data in that tree has
- 18:31Speaker 2: a parent property. So if your code locates a specific
- 18:35Speaker 2: hyperlink in a tag, but you need to know what
- 18:37Speaker 2: container is actually holding that link, you simply call soup
- 18:41Speaker 2: dot a dot parent. The library instantly traverses up the
- 18:46Speaker 2: tree and hands you the surrounding tag, whether it's a paragraph,
- 18:49Speaker 2: a menu list, or a sidebar.
- 18:51Speaker 1: So we have the map. Does that mean we throw
- 18:53Speaker 1: our rejecs compass in the trash?
- 18:55Speaker 2: Not at all. The two tools are meant to be combined.
- 18:58Speaker 2: Beautiful Soup is vastly superior for the heavy lifting of
- 19:01Speaker 2: structural navigation because it understands the architecture of the web page,
- 19:05Speaker 2: but ridjex remains the undisputed king of highly specific string extraction.
- 19:10Speaker 2: I see think about your city analogy. You use Beautiful
- 19:12Speaker 2: Soup's map to confidently navigate down the street, enter the
- 19:15Speaker 2: correct house, walk down the hallway, and step into the
- 19:18Speaker 2: exact right room. Okay, And then once you are safely
- 19:21Speaker 2: in that room, you pull out your regular expressions compass
- 19:23Speaker 2: to search the drawers for the specific item you need.
- 19:26Speaker 1: That makes total sense. You use the tools for what
- 19:28Speaker 1: they're best at. Let's look at the journey we've mapped
- 19:31Speaker 1: out today. We started by sending our robot Butler out
- 19:34Speaker 1: using the requests library, and we learned how to spoof
- 19:37Speaker 1: our headers to slip past servers trying to block us.
- 19:40Speaker 1: Then we explored the remodule, learning how to assemble special
- 19:43Speaker 1: sequences into powerful rejects blueprints to find hidden data. And
- 19:47Speaker 1: finally we realized that to build robust, break resistant scrapers,
- 19:52Speaker 1: we need the parsers behind Beautiful Soup to map the
- 19:55Speaker 1: HTML matrix for us.
- 19:56Speaker 2: You are basically stacking these tools to give yourself total
- 19:59Speaker 2: control over the raw data of the web.
- 20:01Speaker 1: Before we wrap up this deep dive, we'll want to
- 20:04Speaker 1: leave you with a mental exercise to really lock these
- 20:06Speaker 1: concepts in definitely.
- 20:08Speaker 2: Here's a quick challenge to test your understanding of how
- 20:10Speaker 2: these tools combine. Imagine you were scraping a web page
- 20:14Speaker 2: that contains one hundred product reviews. The page itself is
- 20:18Speaker 2: visually cluttered. It is navigation menus, footers, sidebars, advertising banners.
- 20:23Speaker 2: How would you extract only the five star ratings?
- 20:25Speaker 1: Oh, that's a good one.
- 20:26Speaker 2: Think about it logically using the room and drawer concept. First,
- 20:31Speaker 2: you leverage Beautiful soups structural awareness. You use it to
- 20:35Speaker 2: navigate straight past the menus and footers directly into the
- 20:38Speaker 2: paragraph tags that contain the reviews. You navigate to the
- 20:41Speaker 2: right room.
- 20:42Speaker 1: By passing all the junk exactly.
- 20:44Speaker 2: Then, once you have isolated just that review text, you
- 20:48Speaker 2: apply a RIDGEX pattern like backslash, dash, ass star to
- 20:51Speaker 2: comb through the text and extract only the precise ratings
- 20:54Speaker 2: You search the drawer, use.
- 20:56Speaker 1: The map to navigate the maze, use the compass to
- 20:58Speaker 1: extract the treasure. Love. The absolute best way to internalize
- 21:02Speaker 1: this is to get your hands dirty. Open up a
- 21:04Speaker 1: Python terminal, import those libraries, and start exploring the hidden
- 21:09Speaker 1: data structures of your favorite website.
- 21:10Speaker 2: Absolutely, just go build something.
- 21:12Speaker 1: There is a massive, incredibly valuable world of data waiting
- 21:15Speaker 1: to be unlocked. We'll see you on the next deep dive.