Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 3: Mastering CSS, XPath, and Developer Tools
Transcript
- 0:00Speaker 1: Imagine you have this incredible superpower, Right, you can instantly
- 0:06Speaker 1: download any web page on the entire Internet directly to
- 0:09Speaker 1: your computer. Oh wow, yeah, like every article, every store listing,
- 0:12Speaker 1: every piece of public financial data. It sounds amazing, But
- 0:16Speaker 1: then you actually open that download and you are immediately
- 0:19Speaker 1: faced with a massive, impenetrable wall of code.
- 0:23Speaker 2: Right, it's just a mess.
- 0:24Speaker 1: Exactly millions of characters, nested brackets, and just endless blocks
- 0:29Speaker 1: of text. How do you actually find and pull out
- 0:32Speaker 1: the one specific, decimal point of data you wanted from
- 0:37Speaker 1: that absolute chaos?
- 0:38Speaker 2: Yeah? It is basically the digital equivalent of looking for
- 0:41Speaker 2: a needle in a haystack, except the haystack is constantly shifted.
- 0:44Speaker 1: Oh that's a good way to put it.
- 0:45Speaker 2: And it's written in a structural language you might barely speak.
- 0:48Speaker 2: I mean, without the right approach, it can be incredibly
- 0:50Speaker 2: intimidating to look at raw web architecture.
- 0:52Speaker 1: It really is. So for this deep dive, our mission
- 0:55Speaker 1: today is to trade that messy, chaotic net for a
- 0:58Speaker 1: precision spear like that analogy, because I mean, trying to
- 1:02Speaker 1: get data from a web page without knowing how to
- 1:05Speaker 1: actually target it, it means you throw this giant net
- 1:08Speaker 1: out there, and sure you might catch the data point
- 1:10Speaker 1: you wanted.
- 1:11Speaker 2: But you get everything else too.
- 1:12Speaker 1: Right, You're dragging up formatting, code, navigation bars, and just
- 1:17Speaker 1: a ton of digital garbage that you now have to
- 1:19Speaker 1: sort through manually. Yeah.
- 1:20Speaker 2: And whether you are say a data scientist looking to
- 1:24Speaker 2: analyze trends, or a coder automating a workflow, or even
- 1:29Speaker 2: just someone who is insanely curious about how the web actually.
- 1:33Speaker 1: Works, which is a lot of people, honestly.
- 1:35Speaker 2: Oh absolutely, the absolute key to successful webscraping is being
- 1:39Speaker 2: fiercely specific. In the data engineering world, we often think
- 1:42Speaker 2: of this as a learning the sequel of webscraping.
- 1:46Speaker 1: Okay, let's unpack this. Yes, cycle, so structured query language.
- 1:49Speaker 1: That's the tool people use to pull very specific rows
- 1:52Speaker 1: and columns out of highly organized databases.
- 1:54Speaker 2: Right right, exactly. But in a traditional database data is
- 1:58Speaker 2: neat and tidy. The web is not.
- 2:00Speaker 1: No, definitely not.
- 2:00Speaker 2: The web is essentially a giant filing cabinet where someone
- 2:04Speaker 2: just threw the folders in completely haphazardly.
- 2:07Speaker 1: Just papers flying everywhere.
- 2:08Speaker 2: Well pretty much. But by learning how to use HTML
- 2:11Speaker 2: and CSS selectors, you gain that SQL level control over
- 2:16Speaker 2: a really messy web page. You learn how to tell
- 2:19Speaker 2: your computer, you know, ignore the sideheader, ignore the sidebar ads,
- 2:23Speaker 2: just grab the price of this specific used car. Right.
- 2:26Speaker 1: Okay, so we all know web pages are built on
- 2:28Speaker 1: standard HTML tags, like you're each one's for headers, your
- 2:32Speaker 1: list for list items out.
- 2:33Speaker 2: Of basic building blocks.
- 2:34Speaker 1: But if I tell a scraper to just grab a
- 2:36Speaker 1: lie tag on a car dealership website, I'm going to
- 2:39Speaker 1: get a massive array containing every single list item on
- 2:43Speaker 1: that page.
- 2:43Speaker 2: You'll get the cars, the menu, buttons, the footer links.
- 2:46Speaker 1: Everything exactly. So to get that precision speer, we have
- 2:49Speaker 1: to look at how developers label those tags.
- 2:51Speaker 2: And this is where we basically piggyback on the work
- 2:53Speaker 2: of web developers. Developers use CSS, which is cascading style
- 2:57Speaker 2: sheets to make web pages actually look.
- 2:59Speaker 1: Good, to give them color and layout and all.
- 3:01Speaker 2: That right, and to apply those visual styles, they have
- 3:04Speaker 2: to attach specific labels to the HTML framework. So as
- 3:08Speaker 2: data extractors, we use those exact same labels to pinpoint
- 3:11Speaker 2: our data.
- 3:12Speaker 1: Oh that's clever.
- 3:13Speaker 2: Yeah. The two most critical labels in your arsenal are
- 3:16Speaker 2: IDs and classes.
- 3:18Speaker 1: Okay, IDs and classes. Let's start with IDs. How do
- 3:20Speaker 1: those work?
- 3:21Speaker 2: So an ID in HTML is designed by rule to
- 3:24Speaker 2: be completely unique. Think of it like a vehicle identification
- 3:28Speaker 2: number a VIN, or like a social security number.
- 3:32Speaker 1: Got it.
- 3:32Speaker 2: There should only ever be one element on that entire
- 3:35Speaker 2: web page with that specific ID. And in CSS selector syntax,
- 3:40Speaker 2: which is just how we write our instructions, an ID
- 3:42Speaker 2: is represented by a hash symbol.
- 3:45Speaker 1: So if I'm looking at the structural code and I
- 3:47Speaker 1: see hash vin three eight two seven, that hash symbol
- 3:51Speaker 1: instantly tells me this is an ID and is the
- 3:53Speaker 1: only one of its kind on this entire page exactly.
- 3:56Speaker 2: If you use that as your selector, you are guaranteed
- 3:58Speaker 2: to get that one specific car liss and literally nothing else.
- 4:01Speaker 1: You've isolated the exact folder in the filing cabinet, right.
- 4:05Speaker 2: But as you've probably noticed when looking at source code,
- 4:07Speaker 2: developers don't assign a unique ID to every single piece
- 4:10Speaker 2: of text.
- 4:11Speaker 1: That would take forever.
- 4:12Speaker 2: It would be exhausting to code, so they don't do it,
- 4:14Speaker 2: which brings us to yes. In the syntax, a class
- 4:18Speaker 2: is denoted by a dot or a period, so dot
- 4:22Speaker 2: autolisting refers to a class called autolisting, and unlike those
- 4:27Speaker 2: highly unique IDs, classes act a lot more like uniforms.
- 4:31Speaker 1: Uniforms like they group things together.
- 4:32Speaker 2: Exactly, they group multiple related items together. Developers use classes
- 4:37Speaker 2: because they want all the car listings to have the
- 4:39Speaker 2: same visual style.
- 4:40Speaker 1: Oh so like maybe a blue border and a specific
- 4:43Speaker 1: font size for every car.
- 4:45Speaker 2: Right, So instead of styling them one by one, they
- 4:47Speaker 2: just write a rule saying everything wearing the dot autolisting
- 4:51Speaker 2: uniform gets a blue border.
- 4:52Speaker 1: That makes a lot of sense. So if you use
- 4:54Speaker 1: dot autolisting as your scraper selector, you will pull back
- 4:57Speaker 1: every single element on the page wearing that UNI.
- 5:00Speaker 2: Yes, you'll get all the cars, But say I.
- 5:02Speaker 1: Need to be even more precise, Like I don't want
- 5:04Speaker 1: all the listings, and maybe the specific data point I
- 5:06Speaker 1: want doesn't have a unique idea of its own, but
- 5:09Speaker 1: it lives inside a box that does.
- 5:10Speaker 2: Oh okay, Then you combine these concepts using what we
- 5:13Speaker 2: call a parent child hierarchy. Parent child right, an element
- 5:17Speaker 2: that lives inside another element is a child. For example,
- 5:20Speaker 2: a whole list of cars is usually wrapped in a
- 5:23Speaker 2: hull tag. That's an unordered list.
- 5:25Speaker 1: Okay, so the old is the parent exactly.
- 5:28Speaker 2: And the individual items inside are the children. So you
- 5:32Speaker 2: just string your selectors together. To drill down. You might
- 5:34Speaker 2: write old dot listings, then a space, then hash vim
- 5:40Speaker 2: three eight two seven.
- 5:41Speaker 1: Wait, let me make sure I'm translating to the logic here.
- 5:43Speaker 1: So first the scraper finds the unordered list tag that
- 5:46Speaker 1: has the class listings. That's the all dot listings part direct.
- 5:49Speaker 1: Then the space ax is an instruction to like look
- 5:52Speaker 1: inside that specific parent container.
- 5:53Speaker 2: Yeah, you're stepping inside the box.
- 5:55Speaker 1: And finally hash vin three eight two seven means find
- 5:59Speaker 1: the exact child item with this unique ID.
- 6:01Speaker 2: You got it. You are drilling down, step by step,
- 6:04Speaker 2: stripping away the noise of the rest of the page.
- 6:06Speaker 2: Real world EAHTML is incredibly dense, right, So combining tags, classes,
- 6:10Speaker 2: and IDs is how you build a map directly to
- 6:12Speaker 2: your target.
- 6:12Speaker 1: Knowing these labels, the hashes, the dots, the parent child relationships.
- 6:16Speaker 1: It makes perfect sense conceptually, but how do we actually
- 6:19Speaker 1: put this to work? What do you mean? Like, if
- 6:21Speaker 1: I'm sitting at my computer trying to automate this data extraction,
- 6:25Speaker 1: I have to feed these instructions into a program somehow.
- 6:27Speaker 2: Oh right. Yeah. So in Python, which is pretty much
- 6:30Speaker 2: the dominant language for data engineering, the gold standard tool
- 6:34Speaker 2: for this is a library called beautiful soup. It essentially
- 6:37Speaker 2: acts as a.
- 6:37Speaker 1: Parser beautiful soup. It takes the chaotic alphabet soup of
- 6:41Speaker 1: HTML and makes it beautiful. I've always really appreciated that
- 6:45Speaker 1: naming convention.
- 6:46Speaker 2: It is very fitting, isn't it. So first your script
- 6:49Speaker 2: captures the raw HTML text of the page, usually by
- 6:52Speaker 2: sending an HTDP request to the website.
- 6:55Speaker 1: Just downloading the raw tex right.
- 6:56Speaker 2: Then you pass that raw text into beautiful soup, and
- 6:59Speaker 2: the library transforms it into a structured object in your
- 7:03Speaker 2: computer's memory. By convention, developers literally just name this object soup.
- 7:08Speaker 1: So instead of a giant flat wall of text, I
- 7:11Speaker 1: now have an interactive soup object that actually understands the
- 7:15Speaker 1: parent child structure.
- 7:16Speaker 2: Of the page exactly, and you can instantly run commands
- 7:18Speaker 2: on it. Like if you use the method soup dot
- 7:20Speaker 2: findal and pass in your CSS selector say dot autolisting,
- 7:24Speaker 2: beautiful soup will scan the entire structured object.
- 7:28Speaker 1: It finds every single tag wearing that class uniform yeah.
- 7:31Speaker 2: And it neatly packages them into a Python list for you,
- 7:34Speaker 2: so you can analyze it or export it to a
- 7:37Speaker 2: spreadsheet or whatever you need.
- 7:38Speaker 1: That feels incredibly intuitive. It's just Python syntax wrapped around
- 7:42Speaker 1: the developer's CSS labels.
- 7:44Speaker 2: It is really clean.
- 7:45Speaker 1: But you know, I was looking into this and I
- 7:47Speaker 1: noticed a pretty big limitation. Beautiful soup when using CSS
- 7:51Speaker 1: selectors is fantastic for drilling down, like going from parent
- 7:55Speaker 1: to child, right, but it seems to really struggle if
- 7:58Speaker 1: you need to work backward. Say I find a specific price,
- 8:02Speaker 1: but I need to figure out which dealership container it
- 8:04Speaker 1: belongs to, which is higher up? How do you solve that?
- 8:07Speaker 2: Yeah, that is a very common hurdle, and it requires
- 8:09Speaker 2: a fundamental shift in how you navigate the document. CSS
- 8:13Speaker 2: selectors are great for jumping straight to a specific label, right,
- 8:16Speaker 2: but if you need complex relational navigation, like working backward,
- 8:20Speaker 2: you use a different language called XPath. It comes from
- 8:23Speaker 2: the XML world XPath.
- 8:25Speaker 1: I know it stands for XML path language, but how
- 8:28Speaker 1: does it actually change the way we interact with the code?
- 8:30Speaker 2: So XPath forces you to stop thinking about HTML as
- 8:33Speaker 2: a text document with labels and start viewing it strictly
- 8:36Speaker 2: as a tree structure. A tree structure like branching out
- 8:40Speaker 2: Exactly at the very top of the root is the
- 8:42Speaker 2: core HTML tag. That route splits into main branches like
- 8:46Speaker 2: the head and the body.
- 8:47Speaker 1: Okay, cracking so far.
- 8:49Speaker 2: The body branch splits into a header and maybe a
- 8:51Speaker 2: DIV container, and that container branches into individual list items.
- 8:56Speaker 1: Okay, I can visualize that. It's basically like a massive
- 8:59Speaker 1: family tree of data.
- 9:00Speaker 2: That's a perfect way to look at it, rather than
- 9:03Speaker 2: just looking for a class name anywhere. XPath uses path
- 9:06Speaker 2: expressions to actually climb around this tree.
- 9:08Speaker 1: How does that look in the code?
- 9:09Speaker 2: Well, if you want to find all list items anywhere
- 9:12Speaker 2: on the page, you don't just type Y in XPath,
- 9:15Speaker 2: you write slash.
- 9:17Speaker 1: Lye oh, double slack.
- 9:18Speaker 2: Yeah, the double slash is shorthand for start at the route,
- 9:21Speaker 2: search down every single branch, and give me every light
- 9:23Speaker 2: tag you find.
- 9:24Speaker 1: That kind of gives us the aha moment for why
- 9:27Speaker 1: XPath is so powerful for complex scraping.
- 9:30Speaker 2: Yeah, how so, think about.
- 9:32Speaker 1: An out of stock badge on an e commerce site.
- 9:35Speaker 1: With CSS, you can easily script the page and find
- 9:38Speaker 1: all the badges that say out of stock. Sure, but
- 9:41Speaker 1: CSS can't easily tell you what product that badge actually
- 9:44Speaker 1: belongs to because the product name is located higher up
- 9:47Speaker 1: in the code exactly.
- 9:48Speaker 2: The product name is a parent or maybe a sibling branch.
- 9:53Speaker 1: In the tree.
- 9:54Speaker 2: CSS really struggles to climb.
- 9:56Speaker 1: Up like that, So how does XPath handle it?
- 9:59Speaker 2: With X path, you can locate the out stock badge
- 10:02Speaker 2: on its branch and then write an expression that explicitly says,
- 10:06Speaker 2: now travel backward up the tree one level, go down
- 10:08Speaker 2: the adjacent branch, and grab the H two tag holding
- 10:11Speaker 2: the product name.
- 10:12Speaker 1: Wow, so beautiful soup with CSS is like telling a friend, hey,
- 10:15Speaker 1: look around for the house with the blue door. You're
- 10:17Speaker 1: just looking for an attribute that's right. But XPath feels
- 10:20Speaker 1: way more like giving them exact GPS coordinates and turn
- 10:23Speaker 1: by turn directions on a map. You are defining the
- 10:26Speaker 1: literal path to the data.
- 10:27Speaker 2: I love that comparison. And the thing is neither approach
- 10:30Speaker 2: is inherently superior. You know they are complementary.
- 10:33Speaker 1: Tools, right, you'd use them for different things.
- 10:35Speaker 2: Yeah. CSS selectors are cleaner, they're less verbose, and usually
- 10:39Speaker 2: they're what you reach for first. But when the HTML
- 10:42Speaker 2: structure is deeply nested and you need relational data like
- 10:46Speaker 2: matching that price to a product name higher up, X
- 10:50Speaker 2: path is the heavy machinery you've born in to traverse
- 10:53Speaker 2: the tree.
- 10:54Speaker 1: I'll be honest, though, whether we are talking about CSS
- 10:57Speaker 1: parent child combinations or XPath tree branches. Handwriting these selectors
- 11:01Speaker 1: from scratch sounds super risky.
- 11:04Speaker 2: It absolutely is.
- 11:05Speaker 1: I mean staring at raws horsecode and trying to manually
- 11:08Speaker 1: map out old dot listing space hash vin three eight
- 11:11Speaker 1: two seven. Yeah, feels like a title waiting to happen.
- 11:14Speaker 2: One missing dot or slash and the.
- 11:16Speaker 1: Whole scraper breaks. Is there a visual shortcut?
- 11:18Speaker 2: Thankfully? Yes, you do not have to guess, and you
- 11:21Speaker 2: certainly don't have to read the raw, unformatted code. You
- 11:23Speaker 2: actually have a secret weapon built right into the browser
- 11:26Speaker 2: you are using right now, really in the browser.
- 11:28Speaker 1: Yeah, all modern web browsers have a suite of developer
- 11:31Speaker 1: tools designed specifically for inspecting page architecture. Oh, the inspect
- 11:36Speaker 1: element feature. I think most people have accidentally hit F
- 11:39Speaker 1: twelve or right clicked and hit inspect and just totally
- 11:42Speaker 1: panic when their screen suddenly split.
- 11:43Speaker 2: In half revealing all that code. Yeah, most people immediately
- 11:46Speaker 2: close it.
- 11:46Speaker 1: Guilty is charged.
- 11:47Speaker 2: But for web scraping, those developer pains are literally your laboratory.
- 11:53Speaker 2: Imagine you are looking at that car dealership web page.
- 11:56Speaker 2: You see the price of a car on the screen,
- 11:58Speaker 2: say twenty five thousand dollar. Okay, you don't open the
- 12:01Speaker 2: source cone and use control F to search for twenty
- 12:04Speaker 2: five thousand. You just hover your mouse pointer directly over
- 12:07Speaker 2: the price on the visual screen, write click and select inspect.
- 12:11Speaker 1: And the browser just splits and it provides a step
- 12:13Speaker 1: by step mapping of that exact element exactly.
- 12:17Speaker 2: The top pain displays the HTML tree and the browser
- 12:20Speaker 2: automatically expands the branches and highlights the exact line of
- 12:23Speaker 2: code that generates that twenty five thousand text.
- 12:26Speaker 1: That's amazing.
- 12:27Speaker 2: And the bottom pain shows you all the CSS rule
- 12:29Speaker 2: applying to it. It instantly reveals the tags, the IDs,
- 12:32Speaker 2: the classes, the exact blueprint the developer used.
- 12:36Speaker 1: There is a really practical trick I saw that completely
- 12:39Speaker 1: blew my mind regarding this. Those developer pains aren't just
- 12:43Speaker 1: read only.
- 12:44Speaker 2: Oh yeah, the editing, right.
- 12:46Speaker 1: You can actually edit the code locally in your browser
- 12:49Speaker 1: to visually debug your scraper before you ever write a
- 12:52Speaker 1: single line of Python.
- 12:53Speaker 2: It is such an incredibly powerful workflow. And just to
- 12:56Speaker 2: clarify for everyone, you are only changing how the page
- 13:00Speaker 2: renders on your specific machine temporarily.
- 13:02Speaker 1: Right, You aren't actually hacking.
- 13:04Speaker 2: The website exactly. But Let's say you inspect the price
- 13:07Speaker 2: and you see it as a class called price tag.
- 13:09Speaker 2: You think, great, dot price tag is my CSS selector
- 13:12Speaker 2: makes sense? But how do you know the developer didn't
- 13:15Speaker 2: also use that exact same class for the prices of
- 13:17Speaker 2: accessories or maybe warranties that are hidden elsewhere on the page.
- 13:21Speaker 1: Right. If you just run the scraper right, then you
- 13:23Speaker 1: might pull back a bunch of garbage data you didn't
- 13:25Speaker 1: expect exactly.
- 13:27Speaker 2: So to verify your selector is perfectly precise, you go
- 13:30Speaker 2: into the CSS pain in your developer tools. You create
- 13:33Speaker 2: a temporary new rule for dot price tag and add
- 13:37Speaker 2: a highly visible style like a background color dot orange.
- 13:41Speaker 1: And instantly, every single element on the web page that
- 13:46Speaker 1: shares that class just lights up bright orange on your screen.
- 13:50Speaker 2: Yes, you just scroll up and down the page. If
- 13:53Speaker 2: only the car prices are orange, your selector is perfect.
- 13:56Speaker 1: But if the navigation bar menu or the footer links
- 13:59Speaker 1: are suddenly.
- 13:59Speaker 2: Orange, then you know your selectors too broad and you
- 14:02Speaker 2: need to drill down further. Maybe by adding a parent tag.
- 14:05Speaker 1: It provides immediate visual conformation. That is so cool. Yeah,
- 14:09Speaker 1: and I guess if you completely mangle the code while
- 14:13Speaker 1: playing around You just hit refresh and the page loads
- 14:16Speaker 1: fresh from the server.
- 14:17Speaker 2: Yep, no harm done. And once you have visually confirmed
- 14:19Speaker 2: the right element, the browser even provides a shortcut for
- 14:22Speaker 2: extracting the path.
- 14:23Speaker 1: Oh this is the best part.
- 14:24Speaker 2: In that HTML pain. You can just right click the
- 14:26Speaker 2: highlighted line of code, go to popy and select poppy
- 14:29Speaker 2: selector or copyx path.
- 14:31Speaker 1: It literally copies the exact string of characters right to
- 14:33Speaker 1: your clipboard. You switch over to your Python script and
- 14:36Speaker 1: hit paste. The browser literally does the heavy lifting of
- 14:40Speaker 1: figuring out the hierarchy.
- 14:41Speaker 2: Yeah, it usually gets you about ninety percent of the
- 14:43Speaker 2: way there. Sometimes you have to tweak the copied path
- 14:46Speaker 2: slightly to make it robust against minor page updates, but
- 14:49Speaker 2: it completely eliminates the typo risk of handwriting complex relational paths.
- 14:53Speaker 1: Bypassing the manual mapping. Is such a brilliant workflow. Yeah,
- 14:57Speaker 1: but okay, let's step away from the simplified car list
- 15:00Speaker 1: example and introduce some real world chaos.
- 15:03Speaker 2: I love chaos.
- 15:04Speaker 1: Because moving from theory to actual massive websites is where
- 15:08Speaker 1: scrapers usually break.
- 15:10Speaker 2: They do they require a much more analytical approach. Let's
- 15:13Speaker 2: look at say a Wikipedia data extraction scenario. Suppose you
- 15:18Speaker 2: want to analyze a company's historical production numbers. You navigate
- 15:21Speaker 2: to the page and you find a massive grid of
- 15:23Speaker 2: data spanning decades.
- 15:25Speaker 1: I know, Wikipedia structured data tables are generally considered a
- 15:28Speaker 1: scraper's dream.
- 15:29Speaker 2: Oh. Absolutely, grids are built using HTML table tags.
- 15:33Speaker 1: So they are inherently structured from the start.
- 15:35Speaker 2: Yes, you write click a cell, inspect it, and look
- 15:38Speaker 2: at the HTML tree. You scroll up slightly to find
- 15:41Speaker 2: the parent tag holding all those rows and columns, which
- 15:43Speaker 2: might have a class like table dot wikitable.
- 15:46Speaker 1: Then you apply the orange background trick Verify.
- 15:49Speaker 2: The whole grid lights up, and then you just pull
- 15:51Speaker 2: that entire parent structure into beautiful soup. Your script can
- 15:55Speaker 2: easily just loop through the rows.
- 15:57Speaker 1: But there's a secondary treasure map hidden in that star
- 16:00Speaker 1: structure that I find even more fascinating. You mean the footnotes, Yes,
- 16:03Speaker 1: the footnotes. When you look at those tables, they are
- 16:06Speaker 1: constantly peppered with little superscript citation numbers, right the reference links.
- 16:12Speaker 1: If you write click and inspects a footnote number one
- 16:14Speaker 1: oh five, you'll find the URL of the primary source,
- 16:18Speaker 1: and often that link bypasses Wikipedia entirely and points directly
- 16:23Speaker 1: to a raw unformatted database API or a corporate press release.
- 16:27Speaker 2: Exactly, once you train your eyes to look at the
- 16:30Speaker 2: underlying structure, you aren't just scraping the summary anymore. You
- 16:33Speaker 2: are using the HTML to map out the raw data
- 16:36Speaker 2: sources in the helves.
- 16:37Speaker 1: It's like inception for data. Now, tables are definitely the
- 16:40Speaker 1: ideal scenario. But I want to throw a curveball at you.
- 16:42Speaker 1: Laid on me. I was looking at a scenario involving
- 16:45Speaker 1: financial data, but this data was trapped inside a visual
- 16:49Speaker 1: bar graph showing market trends over several months.
- 16:52Speaker 2: Oh, okay, visual graphs.
- 16:53Speaker 1: Yeah, I tried to mentally map out how I would
- 16:56Speaker 1: inspect it. But when you inspect a dynamic graph, the
- 16:59Speaker 1: actual numbers literally aren't in the code. How do you
- 17:03Speaker 1: pull data that isn't even there?
- 17:04Speaker 2: Yeah, that is one of the most fascinating challenges in
- 17:07Speaker 2: data extraction. So first, if a graph is just a
- 17:10Speaker 2: flat JPEG image, standard HTML parsing won't work at.
- 17:14Speaker 1: All, right, because it's just a picture.
- 17:17Speaker 2: But modern interactive web graphs are usually built using code
- 17:20Speaker 2: specifically SVEG or scalable vector graphics.
- 17:23Speaker 1: Okay, so it looks like a picture but the browser
- 17:25Speaker 1: is actually drawing it using a mathematical language embedded right
- 17:29Speaker 1: there in the HTM meltree exactly.
- 17:31Speaker 2: So you start your detective work the exact same way.
- 17:33Speaker 2: You hover over one of the columns on the bar
- 17:35Speaker 2: graph and inspect it.
- 17:36Speaker 1: And what do you see?
- 17:37Speaker 2: Instead of text tags like P or H one, you
- 17:40Speaker 2: see SVG specific tags like you'll see a RECTAG, which
- 17:43Speaker 2: stands for a rectangle that represents the visual bar itself,
- 17:46Speaker 2: and those rectangles are grouped into series using G tags.
- 17:50Speaker 1: Okay, so the webpage is literally instructing the browser draw
- 17:53Speaker 1: rectangle right here. But here is where I get stuck.
- 17:56Speaker 1: On these financial sites, you only see the actual value,
- 18:00Speaker 1: say seventy six point four million dollars. When you physically
- 18:04Speaker 1: move your mouse and hover over the visual bar, a
- 18:07Speaker 1: little box pops.
- 18:08Speaker 2: Up with the number right a tooltip.
- 18:10Speaker 1: Yeah, but since our Python script isn't a physical person
- 18:12Speaker 1: moving a physical mouse, that JavaScript pop up never triggers
- 18:16Speaker 1: the number seventy six point four million literally never exists
- 18:20Speaker 1: in the HTML for beautiful.
- 18:22Speaker 2: Soof to find the text data is completely invisible to
- 18:25Speaker 2: the scraper.
- 18:26Speaker 1: So what do you do? Coo?
- 18:27Speaker 2: If you think creatively about the architecture, you realize you
- 18:29Speaker 2: don't actually need the text.
- 18:31Speaker 1: You don't no think about it.
- 18:33Speaker 2: You are looking at instructions for drawing a rectangle. A
- 18:36Speaker 2: rectangle has a whiff, a color, and crucially, a height attribute.
- 18:40Speaker 2: The physical height of the bar on the screen is
- 18:43Speaker 2: mathematically proportional to the financial number it represents.
- 18:46Speaker 1: Oh my god, the height attribute is hard coded into.
- 18:49Speaker 2: The SFGI exactly. Let's walk through the math. You inspect
- 18:52Speaker 2: the visual column for July and you see the SVEG
- 18:55Speaker 2: height attribute is listed as two hundred pixels. And because
- 18:58Speaker 2: you've verified it earlier with your own eyes by hovering
- 19:01Speaker 2: your mouse, you know that two hundred pixel column represents
- 19:04Speaker 2: one thousand dollars.
- 19:05Speaker 1: Okay, so if two hundred pixels equals one thousand dollars,
- 19:07Speaker 1: then divide by two hundred one pixel equals five dollars.
- 19:11Speaker 2: You've just established the ratio. Wow, so you can figure
- 19:14Speaker 2: your scraper to ignore the text entirely. Instead, you use
- 19:18Speaker 2: beautiful soup to extract the SVG height attribute of every
- 19:22Speaker 2: single rectag in that graph.
- 19:24Speaker 1: You just pull back an array of pixel heights like
- 19:26Speaker 1: two hundred hundred and fifty ten exactly.
- 19:29Speaker 2: And once you have those raw heights and Python you
- 19:31Speaker 2: just multiply each one by five dollars. The hot Hummer
- 19:33Speaker 2: pixel bar becomes five hundred dollars, the fifty pixel bar
- 19:36Speaker 2: becomes two hundred and fifty dollars.
- 19:38Speaker 1: That is genius. You bypass the JavaScript pop up entirely
- 19:41Speaker 1: and reverse engineer the underlying data set directly from the
- 19:44Speaker 1: raw geometry of the web page.
- 19:45Speaker 2: It takes analytical thinking, for sure, but that is the
- 19:48Speaker 2: reality of advanced web scraping. The data is almost always
- 19:51Speaker 2: accessible somewhere. You just have to figure out how the
- 19:54Speaker 2: developer wired it to the screen.
- 19:55Speaker 1: Bypassing the text entirely and using geometry is such a
- 19:58Speaker 1: brilliant work around. Shows how this is less about brute
- 20:02Speaker 1: force and much more about structural problem solving. Let's summarize
- 20:05Speaker 1: the journey we've mapped out today. If you want to
- 20:08Speaker 1: extract data from the chaos of the web, you don't
- 20:11Speaker 1: use a tangled net. You use the sequel of the web.
- 20:14Speaker 2: Right.
- 20:14Speaker 1: You target precise HTML tags, unique IDs in grouping classes.
- 20:19Speaker 1: You pull that blueprint into Python using beautiful soup. Or
- 20:23Speaker 1: you navigate complex parent child relationships using the tree traversing
- 20:27Speaker 1: power of x path Yes.
- 20:29Speaker 2: And to avoid typos. You use the browsers inspect element.
- 20:32Speaker 1: Feature, visually testing paths with orange backgrounds.
- 20:36Speaker 2: And copying the exact selectors directly to your clipboard.
- 20:39Speaker 1: And when you face the real world, you play detective,
- 20:43Speaker 1: following table footnotes to raw databases, or utilizing basic math
- 20:47Speaker 1: to scrape the raw geometry of visual graphs.
- 20:50Speaker 2: It is an incredibly potent skill set. It really changes
- 20:53Speaker 2: how you interact with the Internet.
- 20:54Speaker 1: It totally does.
- 20:55Speaker 2: To reinforce what we've covered, I actually have a quick
- 20:57Speaker 2: review exercise for you to track.
- 20:59Speaker 1: Oh awesome, hear.
- 21:00Speaker 2: Think about your absolute favorite news website, you know, the
- 21:03Speaker 2: one you check every morning. If you wanted to build
- 21:05Speaker 2: a script to automatically extract every single news headline on
- 21:09Speaker 2: their home page, would you configure your scraper to look
- 21:13Speaker 2: for an HTML ID or an HTML class.
- 21:17Speaker 1: Oh that's a great practical test.
- 21:19Speaker 2: Just remember the core architectural difference we talked about. IDs
- 21:23Speaker 2: are like social security numbers. They uniquely identify one single
- 21:27Speaker 2: element on a page. Classes are uniforms. They group multiple
- 21:31Speaker 2: items together that share the same layout and style.
- 21:34Speaker 1: So ID or class for a bunch of headlines. I'll
- 21:37Speaker 1: let everyone think about.
- 21:38Speaker 2: That next time you are browsing that news site, right
- 21:40Speaker 2: click one of those headlines, hit inspect, look at the HTML,
- 21:43Speaker 2: pain and see the answer for yourself.
- 21:45Speaker 1: I highly recommend trying that. It instantly demystifies the sites
- 21:48Speaker 1: you use every single day. But you know, as you
- 21:51Speaker 1: start building these precision spears, there's a final thought I
- 21:53Speaker 1: want to leave you with. What's that You now basically
- 21:56Speaker 1: have the technical superpowers to extract almost anything from the web.
- 22:00Speaker 1: But just because you can scrape something, does it mean
- 22:03Speaker 1: you should?
- 22:03Speaker 2: Ah? That's the big question, right.
- 22:06Speaker 1: Pulling public data often really blurs the line between public
- 22:10Speaker 1: accessibility and intellectual property. It opens up a whole world
- 22:14Speaker 1: of ethical and legal boundaries that you really need to
- 22:16Speaker 1: explore and respect.
- 22:18Speaker 2: It's a very powerful tool, and with that comes responsibility exactly.
- 22:21Speaker 1: It's an ongoing, dynamic conversation, not just technically but ethically.
- 22:25Speaker 1: Until next time, keep exploring and keep diving deep.