Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 19: Tree Navigation, Advanced Filtering, and Link Extraction
Transcript
- 0:00Speaker 1: Right now, as you're listening to this, there are just
- 0:03Speaker 1: thousands of invisible bots crawling the way pages you visit
- 0:06Speaker 1: every day, well constantly. They're ripping apart the structural skeleton
- 0:09Speaker 1: of the Internet, extracting data to train massive AI models,
- 0:14Speaker 1: scraping pricing information for hedge funds, and just executing these
- 0:18Speaker 1: automated sweeps to find exposed cybersecurity vulnerabilities.
- 0:22Speaker 2: Right because when you look at a web page, you
- 0:23Speaker 2: see a finished, glossy product.
- 0:26Speaker 1: Exactly, but undernith that visual layer, there was just this chaotic,
- 0:31Speaker 1: sprawling matrix of code.
- 0:33Speaker 2: It is the absolute definition of hidden complexity.
- 0:36Speaker 1: Yeah.
- 0:36Speaker 2: I mean to a normal user, a web browser is
- 0:38Speaker 2: just rendering a beautiful magazine layout. But to a machine,
- 0:42Speaker 2: that page is a highly structured database. Well, assuming you
- 0:45Speaker 2: have the right tools to interrogate it.
- 0:47Speaker 1: You know, welcome to this deep dive today. Our mission
- 0:50Speaker 1: is to basically reverse engineer the mind of the web developer.
- 0:53Speaker 1: We are going to explore how to mentally, visualize, navigate,
- 0:57Speaker 1: and surgically extract the exact data you want from the
- 1:01Speaker 1: raw HTML of a web page.
- 1:02Speaker 2: And we're doing it using one of the most powerful
- 1:04Speaker 2: Python libraries in existence, beautiful soup.
- 1:07Speaker 1: Right, and we were taking this journey entirely through audio,
- 1:10Speaker 1: so no screens required, no walls of text. We're going
- 1:13Speaker 1: to build a really robust mental model together.
- 1:15Speaker 2: By the end of this, you'll be able to turn dense,
- 1:17Speaker 2: overwhelming code into a living, breathing landscape that you can
- 1:23Speaker 2: navigate programmatically.
- 1:24Speaker 1: Okay, let's unpack this, because if we're going to navigate
- 1:27Speaker 1: this landscape, we really need to understand its architecture. A
- 1:30Speaker 1: web page is fundamentally a massive, upside down family tree.
- 1:34Speaker 2: That's a great way to look at it.
- 1:35Speaker 1: At the very top, the ancient ancestor of the entire
- 1:39Speaker 1: document is the HTML tag. Everything else, all the text,
- 1:43Speaker 1: the images, the navigation bars, it all branches down from
- 1:47Speaker 1: that single origin point.
- 1:48Speaker 2: What's fascinating here is that the family tree concept is critical.
- 1:52Speaker 2: I mean understanding it isn't just some theoretical computer science.
- 1:55Speaker 1: Exercise, most highly practical.
- 1:57Speaker 2: Exactly, if you're trying to automate data extractions and you
- 2:00Speaker 2: have to realize that the data isn't just floating in
- 2:02Speaker 2: a vacuum, it is anchored to specific branches of this tree.
- 2:07Speaker 2: Your ability to scrape a site effectively it basically depends
- 2:10Speaker 2: entirely on your ability to traverse those exact branches without
- 2:14Speaker 2: getting lost in the noise.
- 2:15Speaker 1: So let's start with vertical traversal. Before we get into
- 2:18Speaker 1: any advanced filtering, we have to know how to climb
- 2:21Speaker 1: up and down the bloodline. I know beautiful soup has
- 2:25Speaker 1: a dot children property.
- 2:26Speaker 2: Mm hmm.
- 2:27Speaker 1: If I apply dot children to a tag, I'm guessing
- 2:31Speaker 1: it doesn't give me everything inside it, right?
- 2:33Speaker 2: Correct? So the dot children property returns an iterator containing
- 2:37Speaker 2: only the direct immediate children of that specific tag.
- 2:40Speaker 1: Okay, So if you are looking at the top level
- 2:41Speaker 1: HTML tag.
- 2:42Speaker 2: It's immediate children are generally just the head tag and
- 2:45Speaker 2: the body tag.
- 2:46Speaker 1: Wait, what about all the paragraphs and images inside the
- 2:48Speaker 1: body Like? If I am at the HTML tag and
- 2:50Speaker 1: I ask for its children, does it just ignore all
- 2:53Speaker 1: of that data?
- 2:54Speaker 2: Completely ignores it?
- 2:55Speaker 1: Wow?
- 2:55Speaker 2: Think about your own family tree, right? If I ask
- 2:58Speaker 2: you who your children are, you name your sons and daughters.
- 3:02Speaker 2: You don't start listing your grandchildren and great grandchildren, right?
- 3:04Speaker 1: That would be overwhelming.
- 3:05Speaker 2: And the parser works the exact same way. It's highly
- 3:09Speaker 2: memory efficient because it only looks one level deep. So
- 3:12Speaker 2: a paragraph nested inside a divtag which is nested inside
- 3:15Speaker 2: the body tag is essentially a great grandchild of the
- 3:19Speaker 2: HTML root.
- 3:20Speaker 1: Got it.
- 3:21Speaker 2: The dot children property won't even see it.
- 3:23Speaker 1: Okay, but what if I actually need the whole sprawling
- 3:26Speaker 1: ancestry chart, Like if I'm scraping an article, I don't
- 3:30Speaker 1: just want the top level container. I want every single
- 3:33Speaker 1: piece of text nested all the way down.
- 3:35Speaker 2: Well, that is when you swap dot children for the
- 3:37Speaker 2: dot descendant's properties descendence. Okay, Yeah, calling dot descendants forces
- 3:41Speaker 2: the parser to recursively travel down every single branch until
- 3:44Speaker 2: it hits a dead end.
- 3:45Speaker 1: Oh so just keeps going exactly.
- 3:47Speaker 2: It gives you the children, then it opens up those
- 3:49Speaker 2: children and grabs the grandchildren. Just drilling all the way
- 3:53Speaker 2: down to the deepest nested elements, which we call the
- 3:55Speaker 2: leaf nodes.
- 3:56Speaker 1: I can see how that would be compucationally heavier, but
- 3:59Speaker 1: absolutely necessary if you're trying to flatten a really complex document,
- 4:04Speaker 1: and I assume we can travel in.
- 4:06Speaker 2: Reverse, you can. Yeah, every single tag in the parstree
- 4:09Speaker 2: has a dot parent property.
- 4:11Speaker 1: Makes sense.
- 4:11Speaker 2: So if you find an image you want and you
- 4:13Speaker 2: need to know what container it sits inside, calling dot
- 4:16Speaker 2: parent steps you exactly one level up the hierarchy.
- 4:19Speaker 1: And is there a plural version like with descendants.
- 4:23Speaker 2: Yes, just like dot descendants, there is a dot parent's
- 4:26Speaker 2: property plural that will yield a generator stepping all the
- 4:30Speaker 2: way back up to the HTML ancestor.
- 4:33Speaker 1: Now. I was looking into this earlier and I found
- 4:35Speaker 1: a really strange technical quirk with the parent property.
- 4:38Speaker 2: Oh, the root node thing.
- 4:39Speaker 1: Yeah, if you step all the way up to the
- 4:41Speaker 1: top HTML tag and you ask for its parent, you'd
- 4:44Speaker 1: expect it to return an error, right because it's the
- 4:46Speaker 1: top of the tree. But it doesn't.
- 4:48Speaker 2: Yeah, that's a fascinating detail about how beautiful Soup constructs
- 4:51Speaker 2: its internal memory model. The immediate parent of the HTML
- 4:55Speaker 2: tag is actually the beautiful Soup document object itself.
- 4:58Speaker 1: Wait, the document itself is a parent.
- 5:00Speaker 2: Yeah, it treats the entire parse file as a master container.
- 5:04Speaker 1: But if I ask for the parent of that Beautiful
- 5:07Speaker 1: Soup document object.
- 5:08Speaker 2: Then you finally get a return value of none. The
- 5:12Speaker 2: document object is the absolute root of the universe for
- 5:14Speaker 2: that specific scrape. It has no creator above it all.
- 5:18Speaker 1: Right, So we can climb up and down the tags.
- 5:20Speaker 1: But let's be real, we usually aren't scraping a site
- 5:23Speaker 1: just to map out a bunch of EMPTYHTML tags.
- 5:26Speaker 2: Now nobody does that.
- 5:27Speaker 1: We're hunting for the actual human readable data trapped inside them,
- 5:32Speaker 1: the text. I know. Beautiful Soup has a dot string
- 5:35Speaker 1: property to extract text, but I've heard it can be
- 5:38Speaker 1: incredibly fragile, Like, why does it break so easily?
- 5:40Speaker 2: It breaks because it demands an absolute lack of ambiguity.
- 5:44Speaker 2: What do you mean, Well, the dot string property will
- 5:46Speaker 2: only extract text if the tag contains exactly one string
- 5:49Speaker 2: of text and literally nothing else. Okay, let's say you
- 5:52Speaker 2: have a paragraph tag and inside it is a solid
- 5:54Speaker 2: block of text. Dot string works perfectly. But what if
- 5:57Speaker 2: the developer bolted a single word in the middle of
- 5:59Speaker 2: that paragraph?
- 6:00Speaker 1: Oh, I see the problem. The moment they add a
- 6:04Speaker 1: bold tag, the paragraph is no longer just holding a
- 6:07Speaker 1: single string. It's holding a string of normal text than
- 6:11Speaker 1: a nested bowld tag holding its own string and then
- 6:14Speaker 1: more normal text.
- 6:15Speaker 2: Exactly So, when you ask beautiful Soup for the dot
- 6:17Speaker 2: string of that paragraph, it looks the nested chaos gets
- 6:20Speaker 2: confused about which specific piece of text you want, and
- 6:23Speaker 2: instead of guessing, it just gives up.
- 6:25Speaker 1: It just returns none.
- 6:27Speaker 2: Yeah, it completely refuses to assume your intent.
- 6:30Speaker 1: That feels like a nightmare for real world scraping. Honestly,
- 6:33Speaker 1: because web code is incredibly messy, developers are constantly throwing
- 6:38Speaker 1: in spans, links, and formatting tags. So how do we
- 6:41Speaker 1: force it to just give us all the text regardless
- 6:44Speaker 1: of the formatting.
- 6:45Speaker 2: You use the dot strings property plural okay. This tells
- 6:49Speaker 2: the parser to recursively dive into the tag, ignore all
- 6:52Speaker 2: the structural HTML boundaries, and yield every single piece of
- 6:56Speaker 2: text it finds, piece by piece.
- 6:58Speaker 1: Well wait, if it's blindly grabbing every string, isn't it
- 7:00Speaker 1: going to pull in all the invisible junk Like developers
- 7:04Speaker 1: use cabs, carriage returns, and empty spaces just to format
- 7:07Speaker 1: their code so it looks pretty in their editor. Beautiful
- 7:10Speaker 1: soup is going to see all that white space as
- 7:12Speaker 1: valid text strings.
- 7:13Speaker 2: Right, It absolutely does. If you just use dot strings,
- 7:16Speaker 2: your data set will be polluted with thousands of useless
- 7:19Speaker 2: new line.
- 7:20Speaker 1: Characters, which is awful for data analysis.
- 7:22Speaker 2: Which brings us to the ultimate cleanup tool, dot stripped
- 7:26Speaker 2: underscore strings. This property does all the heavy lifting for you.
- 7:30Speaker 2: It extracts all the text, but before it hands it back,
- 7:33Speaker 2: it runs an algorithm to strip away all the leading
- 7:36Speaker 2: and trailing white space.
- 7:37Speaker 1: So it sanitizes it on the fly.
- 7:39Speaker 2: Right, And if a string consists entirely of empty space,
- 7:42Speaker 2: it just deletes it entirely, leaving you with raw, clean.
- 7:45Speaker 1: Data that is really powerful. Okay, so moving vertically makes
- 7:49Speaker 1: sense for hierarchical data. But let's pivot because vertical navigation
- 7:53Speaker 1: has a massive blind spot. It does. What if I
- 7:55Speaker 1: am scraping a massive financial table. All the stock tickers
- 7:58Speaker 1: and prices are sitting side by side in individual cells.
- 8:02Speaker 1: If I only move vertically, I hit a dead end
- 8:04Speaker 1: at the first cell.
- 8:05Speaker 2: Yeah, that's the limitation of the bloodline model. To master
- 8:08Speaker 2: this landscape, you have to learn how to move horizontally.
- 8:11Speaker 2: In the structural parse tree, elements that exist at the
- 8:14Speaker 2: exact same level of nesting are called siblings.
- 8:17Speaker 1: Okay, let's visualize this so we don't get lost. Imagine
- 8:20Speaker 1: a developer writes a single anchor link. Sure, inside that
- 8:23Speaker 1: link they put three distinct elements in a row. First,
- 8:28Speaker 1: a bold tag with the word tom next to it, in,
- 8:31Speaker 1: a talic tag with the word Jerry, and finally a
- 8:34Speaker 1: standard text string that says iconic paar.
- 8:37Speaker 2: Right. So, in the memory of the parser that bold
- 8:40Speaker 2: tag for Tom, the italic tag for Jerry, and the
- 8:43Speaker 2: raw text for iconic pair. They are all immediate children
- 8:46Speaker 2: of the anchor.
- 8:47Speaker 1: Link because they're side by side exactly.
- 8:49Speaker 2: They share the same parent, which means they are siblings.
- 8:52Speaker 1: So if my code is currently parked on the bold
- 8:54Speaker 1: tag for Tom, I don't want to go up to
- 8:56Speaker 1: the parent or down into the text. I just call
- 8:58Speaker 1: the dot next sibling command and it hops perfectly sideways
- 9:01Speaker 1: to the italic tag for Jerry.
- 9:03Speaker 2: Yes, and you can shame these commands or use dot
- 9:05Speaker 2: previous sibling to reverse course. But this is where the
- 9:09Speaker 2: mental model gets really tricky, because beautiful Soup also gives
- 9:13Speaker 2: you commands called dot next element and dot previous element.
- 9:17Speaker 1: I'm going to push back on that right now. I'm
- 9:19Speaker 1: looking at the conceptual model in my head, and isn't
- 9:22Speaker 1: as sibling the exact same thing as the next element?
- 9:25Speaker 2: Not quite?
- 9:26Speaker 1: I mean, if Jerry is next to Tom, then Jerry's
- 9:28Speaker 1: the next element. Why do we need a separate command
- 9:31Speaker 1: for this? It feels completely redundant.
- 9:33Speaker 2: It totally feels redundant until you understand how a parser
- 9:36Speaker 2: actually reads a file into memory. This is the difference
- 9:39Speaker 2: between structural geography and chronological sequence.
- 9:42Speaker 1: Okay, explain that well.
- 9:44Speaker 2: Dot next sibling respects the geography of the family tree.
- 9:47Speaker 2: It stays on the exact same horizontal plane, hopping from
- 9:49Speaker 2: container to container.
- 9:50Speaker 1: Right.
- 9:51Speaker 2: But Dot next element doesn't care about the family tree
- 9:54Speaker 2: at all. It reads the raw HTML code like a book,
- 9:58Speaker 2: line by line exactly in the sequence.
- 10:01Speaker 1: The developer typed it, Oh, let me process that. So
- 10:04Speaker 1: if I am on the bold tag for Tom, the
- 10:06Speaker 1: next structural sibling is the italic tag for Jerry. But
- 10:10Speaker 1: if I ask for the next element, you're saying, I
- 10:13Speaker 1: don't go to Jerry. Where do I go?
- 10:15Speaker 2: You go down? Remember, the bold tag itself isn't the text,
- 10:18Speaker 2: It's just a container. When the parser reads the code chronologically,
- 10:22Speaker 2: it encounters the opening boldtag first what is the very
- 10:25Speaker 2: next thing, and encounters in the code the actual word
- 10:28Speaker 2: Tom exactly. So Dot next element dives into the tag
- 10:32Speaker 2: to grab the text itself. What beautiful Soup calls the
- 10:35Speaker 2: navigable string node.
- 10:36Speaker 1: Oh wow.
- 10:37Speaker 2: The text inside the tag is treated as its own
- 10:40Speaker 2: distinct entity in memory. So from the boldtag, Dot next
- 10:44Speaker 2: element drops down to the text Tom.
- 10:46Speaker 1: And if you call it again.
- 10:47Speaker 2: If you call dot next element again. It moves up
- 10:49Speaker 2: and out of the bold tag and finally hits the
- 10:51Speaker 2: italic tag, then the text Jerry, it is a strict
- 10:56Speaker 2: chronological breadcrumb trail of how the document was written.
- 10:59Speaker 1: That is a phenomenal stinction siblings for horizontal structure, elements
- 11:03Speaker 1: for strict chronological reading order. But as brilliant as all
- 11:07Speaker 1: this manual navigation is, we really have to acknowledge a
- 11:10Speaker 1: harsh reality here. Yes, scale, If I am scraping a
- 11:14Speaker 1: fifty thousand line Amazon product page, writing a script that
- 11:19Speaker 1: steps sibling by sibling through the tree is going to
- 11:22Speaker 1: be incredibly slow and fragile. I mean, if Amazon changes
- 11:26Speaker 1: a single divtag, my whole script breaks.
- 11:28Speaker 2: Yeah, manual navigation is necessary for highly localized extraction, but
- 11:32Speaker 2: it is an absolute nightmare at scale. You don't want
- 11:34Speaker 2: to walk. You want to teleport directly to the data
- 11:37Speaker 2: you care about.
- 11:37Speaker 1: Teleporting sounds much.
- 11:38Speaker 2: Better, and that introduces the core filtering engine of Beautiful Soup.
- 11:43Speaker 2: Dot find and dot find all.
- 11:45Speaker 1: These are the heavy hitters. If I use dot find,
- 11:47Speaker 1: the parser scans the document and hands me the very
- 11:50Speaker 1: first match it sees, then stops right. But dot find
- 11:53Speaker 1: all is like a vacuum. It scans the entire tree,
- 11:56Speaker 1: gathers every single instance that matches my criteria, and hands
- 11:59Speaker 1: them back to me in a Python list.
- 12:01Speaker 2: And the flexibility of these filters is really what makes
- 12:03Speaker 2: the library and industry standard. I mean, the simplest filter
- 12:06Speaker 2: is just by HTML tag. You pass the string p
- 12:09Speaker 2: into dot find all and instantly you have a list
- 12:11Speaker 2: of every single paragraph on the page.
- 12:13Speaker 1: But paragraphs are generic. We usually want something highly specific.
- 12:17Speaker 1: I can filter by attributes, right.
- 12:19Speaker 2: You definitely can.
- 12:19Speaker 1: Like if I only want profile pictures, I don't want
- 12:21Speaker 1: all images, I can tell dot find all to look
- 12:24Speaker 1: for an image tag where the src attributes specifically equals
- 12:28Speaker 1: profile dot jpg. Or if I want to cast a
- 12:31Speaker 1: wider net, I can say, hey, find me all anchor
- 12:34Speaker 1: tags where an href attribute simply exists regardless of where
- 12:38Speaker 1: it points. You just pass in href equals true.
- 12:41Speaker 2: That boolean logic is incredibly useful for stripping out deadlinks.
- 12:46Speaker 2: But we have to talk about the most notorious trap
- 12:48Speaker 2: in beautiful soup filtering. It involves CSS classes.
- 12:52Speaker 1: Oh man, here's where it gets really interesting. This is
- 12:54Speaker 1: like a rite of passage for every developer.
- 12:57Speaker 2: It really is, and.
- 12:58Speaker 1: Webdev developers constantly use classes to style their tags. A
- 13:02Speaker 1: warning banner might have a class called alert. But if
- 13:05Speaker 1: you write your Python filter and literally type class equals alert,
- 13:09Speaker 1: the entire program catastrophically crashes before it even runs. Yeah,
- 13:13Speaker 1: why does Python lose its mind over the word.
- 13:15Speaker 2: Class Because you are fighting the core architecture of the
- 13:17Speaker 2: Python language itself. In Python, the word class is a reserved,
- 13:23Speaker 2: deeply protected keywords.
- 13:25Speaker 1: Used for object oriented programming.
- 13:26Speaker 2: Right exactly. It's the command used to define object blueprints.
- 13:30Speaker 2: So when you type class equals alert as an argument
- 13:34Speaker 2: in your filter, the Python interpreter intercepts it before beautiful
- 13:37Speaker 2: Soup even gets a chance to see it.
- 13:39Speaker 1: It thinks you're writing a new class.
- 13:41Speaker 2: Python thinks you are trying to invent a new object blueprint.
- 13:44Speaker 2: Right there in the function call realizes the syntax makes
- 13:47Speaker 2: no sense and throws a fatal error.
- 13:49Speaker 1: It's a classic names based collision. So to get around it,
- 13:52Speaker 1: Beautiful Soup implemented this tiny, invisible.
- 13:55Speaker 2: Hack the underscore.
- 13:57Speaker 1: Yeah, you have to use class underscore with an underscore
- 13:59Speaker 1: at the end. That trailing underscore tells pipeon, Hey, this
- 14:02Speaker 1: is just a normal argument, Ignore.
- 14:04Speaker 2: It, and beautiful soup swips the underscore off in the
- 14:06Speaker 2: background and searches the HTML correctly. If you don't know
- 14:09Speaker 2: that quirk, you will spend hours staring at syntax errors.
- 14:13Speaker 1: It is the ultimate invisible hurdle. But let's push the
- 14:17Speaker 1: complexity further. What if you are dealing with dynamic, unpredictable data.
- 14:22Speaker 1: You don't know the exact class name. You just know
- 14:24Speaker 1: that all the data you want is trapped in tags
- 14:27Speaker 1: where the ID attribute happens to contain a string of
- 14:29Speaker 1: random numbers.
- 14:30Speaker 2: You can't search for an exact match then.
- 14:32Speaker 1: Right, So that is when you bring in the heavy
- 14:34Speaker 1: artillery regular expressions or rejects. Beautiful Soup doesn't force you
- 14:38Speaker 1: to use exact strings. You can actually pass a compiled
- 14:41Speaker 1: rejects pattern directly into dot find all.
- 14:44Speaker 2: The integration is seamless. You can write a reject using
- 14:47Speaker 2: the carrot symbol and a P like carrot P, and
- 14:50Speaker 2: the filter will dynamically gather any tag that starts with
- 14:53Speaker 2: the letter P, paragraphs, pictures, whatever, wow. Or you can
- 14:57Speaker 2: use the rejects backslash key pattern and it will instantly
- 15:00Speaker 2: sweep the document for any element whose ID contains a digit.
- 15:04Speaker 1: Okay, I'm going to push back again. Though, rejects is
- 15:08Speaker 1: incredibly powerful for pattern matching. But sometimes data isn't hidden
- 15:12Speaker 1: behind a pattern, it's hidden behind bizarre, hyper specific business logic.
- 15:17Speaker 2: That's true.
- 15:17Speaker 1: What if I want to find all image tags that
- 15:20Speaker 1: have a source file, but for some inexplicable reason, they
- 15:23Speaker 1: explicitly do not have an old text attribute. Rejects can't
- 15:27Speaker 1: really express the presence of x and the absolute absence
- 15:32Speaker 1: of why in an HTML parse note easily.
- 15:34Speaker 2: This is where beautiful Soup transitions from just a simple
- 15:38Speaker 2: library into a full scale data engineering platform. Also, when
- 15:42Speaker 2: rejects fails, you can write a bespoke Python function with loops,
- 15:46Speaker 2: conditionals any logic you want and pass that entire function
- 15:50Speaker 2: directly into dot find all.
- 15:52Speaker 1: Wait, I can pass a function as the Silter criteria.
- 15:54Speaker 1: How does that even work? Under the hood?
- 15:56Speaker 2: When you pass a custom function to dot find all,
- 15:58Speaker 2: beautiful soup takes every single tag in the entire document
- 16:02Speaker 2: and feeds it into your function one by one.
- 16:04Speaker 1: Just iterates through them all.
- 16:05Speaker 2: Yeah, your function inspects the tag. If your function returns true,
- 16:09Speaker 2: beautiful sap keeps the tag. If it returns false, it
- 16:12Speaker 2: throws it away.
- 16:13Speaker 1: That sounds computationally heavy.
- 16:15Speaker 2: Oh, it is highly computationally expensive because Python has to
- 16:18Speaker 2: execute logic on every single node. But the tradeoff is
- 16:21Speaker 2: surgical precision.
- 16:22Speaker 1: Give me an example of when you'd use that.
- 16:24Speaker 2: Let's say you are executing a cybersecurity audit on a
- 16:27Speaker 2: massive corporate wiki. You need to find all outgoing links
- 16:32Speaker 2: that specifically do not point to a secure internal domain. Okay,
- 16:36Speaker 2: you write a function that looks at the h fref attribute,
- 16:39Speaker 2: parses the URL string, checks against a list of approved domains,
- 16:43Speaker 2: and only returns true if it's an unapproved external link.
- 16:46Speaker 2: You're executing complex, multi stage logic across millions of lines
- 16:50Speaker 2: of code.
- 16:51Speaker 1: So we have built the mental model. Yeah, we can
- 16:54Speaker 1: navigate vertically and horizontally, and we can teleport and filter
- 16:58Speaker 1: using rajax and custom functions. Let's synthesize all of this
- 17:01Speaker 1: into a high stakes practical scenario. We need to rip
- 17:04Speaker 1: every single link off a live, massive web page.
- 17:07Speaker 2: This is the exact scenario you face when building an
- 17:10Speaker 2: automated web crawler. You pull down the raw HTML, parse
- 17:13Speaker 2: it into the beautiful soup tree, and use find all
- 17:16Speaker 2: to isolate every single anchor tag.
- 17:18Speaker 1: But the filter hands me the entire HTML element. I
- 17:20Speaker 1: don't want the bracket nonsense. I just want the raw
- 17:23Speaker 1: URL string trapped inside it. So I know. I can
- 17:25Speaker 1: treat the isolated tag almost exactly like a Python dictionary.
- 17:28Speaker 1: I use square brackets. Open a quote type h ref,
- 17:31Speaker 1: close the quote, and close the bracket.
- 17:33Speaker 2: YEP. That instantly extracts the raw web a drafts.
- 17:36Speaker 1: That extracts the value. But the moment you start extracting
- 17:39Speaker 1: those URLs at scale, you run into the most dangerous
- 17:43Speaker 1: threat to an automated crawler.
- 17:45Speaker 2: The difference between absolute and relative links.
- 17:48Speaker 1: Oh, relative links are absolute poison for a scraper.
- 17:51Speaker 2: They really are.
- 17:52Speaker 1: If I pull an absolute link, it has the whole
- 17:54Speaker 1: package right, the HTTPS, the main domain, the exact filepath.
- 17:59Speaker 1: My scraper just ping that URL and keep moving.
- 18:02Speaker 2: But a relative link is just a lazy internal shortcut.
- 18:05Speaker 2: The developer just writes slash log in or slash images,
- 18:09Speaker 2: slash logo, dot.
- 18:10Speaker 1: P and e right because the browser understands it exactly.
- 18:13Speaker 2: The browser knows what website you are currently looking at.
- 18:16Speaker 2: But your Python scraper is operating in a vacuum exactly.
- 18:19Speaker 1: If my scraper isolates slash log in and tries to
- 18:22Speaker 1: send a request to that literal string. It crashes instantly
- 18:25Speaker 1: because it has no idea what server on the Internet
- 18:27Speaker 1: to talk to. So how do we programmatically sanitize this
- 18:30Speaker 1: data set on the fly?
- 18:32Speaker 2: First, you separate the clean data from the dirty data.
- 18:35Speaker 2: You can use a regular expression with the pipe operator,
- 18:37Speaker 2: which is just a vertical line acting as an omar statement. Okay,
- 18:41Speaker 2: you write a rejects that isolates links starting with httprhttps.
- 18:45Speaker 2: Those are your absolute links.
- 18:47Speaker 1: They are safe, but for the relative links, we have
- 18:49Speaker 1: to mathematically reconstruct them. Since we know the base URL
- 18:53Speaker 1: of the site we are scraping, we just write a
- 18:55Speaker 1: simple loop just depend it. Yeah, every time we extract
- 18:58Speaker 1: a link that doesn't start with EGT, we intercept it.
- 19:01Speaker 1: We take our base urlsahgtps, example dot com, and we
- 19:05Speaker 1: literally just concatenate it to the front of the relative path.
- 19:07Speaker 2: It is a brilliantly simple programmatic fix. It catches slash login,
- 19:13Speaker 2: slaps the domain on the front, and instantly converts a
- 19:16Speaker 2: completely useless script crashing relative link into a fully qualified
- 19:20Speaker 2: absolute URL.
- 19:22Speaker 1: You just systematically standardize an entirely chaotic data set in milliseconds.
- 19:26Speaker 2: And if we connect this to the bigger picture, this workflow,
- 19:29Speaker 2: navigating the tree, isolating the nodes, extracting the attributes, and
- 19:33Speaker 2: standardizing the relative paths. That is the exact blueprint for
- 19:36Speaker 2: how massive search engines map the Internet, how they see
- 19:40Speaker 2: the Web exactly. It's how vulnerability scanners spider through a network,
- 19:44Speaker 2: finding every hidden page and exposed endpoint. They don't just
- 19:47Speaker 2: read code, they rebuild the map dynamically as they crawl.
- 19:51Speaker 1: We've covered a massive amount of architectural ground today. We
- 19:54Speaker 1: started by mapping the Internet as an upside down family tree,
- 19:58Speaker 1: using dot children for shallow chain and dot descendants to
- 20:01Speaker 1: recursively map the entire bloodline.
- 20:04Speaker 2: We recognize the limitations of vertical traversal two and learn
- 20:07Speaker 2: to move horizontally, understanding the critical parser difference between dot
- 20:12Speaker 2: next sibling for structural leaps and dot next element for
- 20:15Speaker 2: strict chronological reading.
- 20:17Speaker 1: We abandon manual walking for teleportation, utilizing dot find all
- 20:21Speaker 1: to filter by tags, boolliant attributes, and even navigating Python's
- 20:25Speaker 1: keyword collisions with the class underscore quirk.
- 20:27Speaker 2: Then we scale that up using rejects and computationally heavy
- 20:31Speaker 2: custom functions.
- 20:32Speaker 1: And finally we extracted raw dictionary values to programmatically rebuild
- 20:36Speaker 1: relative links. On the fly.
- 20:38Speaker 2: To lock this mental model in place, let's run a
- 20:40Speaker 2: rapid fire review exercise for you listening. Imagine you are
- 20:45Speaker 2: staring at a chaotic difftag. It's filled with nested spans,
- 20:49Speaker 2: empty spaces, and carriage returns left over by a sloppy developer.
- 20:53Speaker 1: Sound familiar.
- 20:54Speaker 2: You want the text, and only the text, completely sanitized.
- 20:58Speaker 2: What specific property do you call to obliterate all that
- 21:01Speaker 2: white space and.
- 21:02Speaker 1: Building on that? If you need to find all tags
- 21:05Speaker 1: that share a CSS class called highlight, how exactly do
- 21:08Speaker 1: you write that argument in your dot find all filter
- 21:11Speaker 1: without crashing the Bython compiler.
- 21:13Speaker 2: Take a mental second to lock in your answers.
- 21:15Speaker 1: Got it. For the tech senitization, you need the dot
- 21:18Speaker 1: stripped underscore strings generator, and for the filter you must
- 21:21Speaker 1: use class underscore. Do not forget the trailing underscore.
- 21:25Speaker 2: If you understand the why and the how behind those tools,
- 21:27Speaker 2: you have immense power to force structure onto completely unstructured data.
- 21:32Speaker 1: But I want to leave you with one final provocative thought,
- 21:34Speaker 1: a completely new rabbit hole. Everything we have unpacked today
- 21:38Speaker 1: relies on a massive.
- 21:39Speaker 2: Assumption that beautiful soup is parsing the static HTML exactly
- 21:43Speaker 2: as it was delivered by the web server over the wire.
- 21:45Speaker 1: Right, it maps the skeleton as it existed at the
- 21:48Speaker 1: precise millisecond of the request.
- 21:50Speaker 2: It is a snapshot of the static dom.
- 21:53Speaker 1: But the modern Internet is rarely static anymore. What happens
- 21:57Speaker 1: to that family tree when a web page is built
- 21:59Speaker 1: on a framework like react or Angular. What happens when
- 22:02Speaker 1: the initial HTML is completely empty and the page relies
- 22:06Speaker 1: entirely on JavaScript to dynamically render its text, its images,
- 22:09Speaker 1: and its links. Seconds after the browser.
- 22:12Speaker 2: Loads, your static parser is just going to look at
- 22:14Speaker 2: an empty room exactly.
- 22:16Speaker 1: It fundamentally shatters the structural model we built today. If
- 22:19Speaker 1: the glossy magazine is painting itself in real time using JavaScript,
- 22:23Speaker 1: how do you scrape a skeleton that hasn't even finished
- 22:26Speaker 1: growing yet. That is a complex architectural challenge for you
- 22:29Speaker 1: to investigate on your own. Keep experimenting, keep pulling back
- 22:32Speaker 1: the layers of the web, and we'll catch you on
- 22:34Speaker 1: the next deep dive.