Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 5: From Environment Setup to Pandas DataFrames
Transcript
- 0:00Speaker 1: Imagine you are working on a really crucial research project.
- 0:05Speaker 1: Maybe you're pulling financial data on S and P five
- 0:08Speaker 1: hundred company, right.
- 0:09Speaker 2: Something where you need it's ton of accurate.
- 0:11Speaker 1: Numbers exactly, And you find the perfect table on a
- 0:14Speaker 1: Wikipedia page or maybe some financial blog. I mean it
- 0:17Speaker 1: has absolutely everything, ticker symbols, historical market caps, revenue growth,
- 0:23Speaker 1: you name it.
- 0:23Speaker 2: But then you start looking around the page.
- 0:25Speaker 1: Yeah, there's no download button, there's no export to CSD anywhere,
- 0:29Speaker 1: just you know, hundreds of rows of a table just
- 0:32Speaker 1: staring right back at you, course feeling it really is.
- 0:35Speaker 1: And I mean you could just highlight the whole thing
- 0:37Speaker 1: with your mouse, copy it and paste it into a spreadsheet.
- 0:40Speaker 1: But we all know how that ends.
- 0:41Speaker 2: Oh yeah, terrible formatting, weird hidden hyperlinks, columns completely misaligned.
- 0:47Speaker 1: Right. You'd spend like the next three hours manually deleting
- 0:50Speaker 1: hidden characters. So the question is do you just surrender
- 0:53Speaker 1: to the mindless manual labor. No, you build a digital
- 0:56Speaker 1: robot to do.
- 0:56Speaker 2: It for you. You automate the extraction, I mean by
- 1:00Speaker 2: writing a script. You bypass all that tedious format and cleanup.
- 1:03Speaker 2: You just let the computer handle the heavy lifting.
- 1:06Speaker 1: And the real advantage isn't just saving time today.
- 1:08Speaker 2: Is it? No, not at all. It's that once the
- 1:10Speaker 2: script is actually written, you can run it again tomorrow
- 1:13Speaker 2: and the next day you just extract updated data in seconds.
- 1:17Speaker 1: So today we are taking you on a completely hands
- 1:20Speaker 1: on journey. This deep dive is a step by step
- 1:23Speaker 1: audio walkthrough of a Python webscraping project.
- 1:26Speaker 2: We're taking you from literally setting up a rock solid
- 1:29Speaker 2: coding environment to downloading a target web page, scrubbing away
- 1:33Speaker 2: all the messy underlying HTML, and just magically dropping pure,
- 1:38Speaker 2: clean data into a panda's data frame.
- 1:40Speaker 1: I like to think of this whole process like a
- 1:42Speaker 1: classic digital heist movie.
- 1:44Speaker 2: A heist Okay, I like that, yeah.
- 1:46Speaker 1: I mean, first you have to assemble your crew, which
- 1:48Speaker 1: in this case means your specific software tools. Then you
- 1:51Speaker 1: have to case the joint right inspecting the web page's
- 1:54Speaker 1: structure without dipping off the security system.
- 1:57Speaker 2: And finally you have to actually get past the vault door,
- 2:00Speaker 2: extract the prize, and make a clean getaway with.
- 2:03Speaker 1: Your data exactly. The Hyst framework just works perfectly here.
- 2:07Speaker 2: It really does, because if you just rush into a
- 2:09Speaker 2: bank without a plan, or in our case, if you
- 2:12Speaker 2: just open a blank text file and start typing Python
- 2:15Speaker 2: code to scrape a site. You are going to trigger
- 2:18Speaker 2: a massive cascade of messy, frustrating problems.
- 2:22Speaker 1: Long before you even download a single bite of data.
- 2:24Speaker 2: Exactly.
- 2:25Speaker 1: So let's start right there, assembling the crew and preparing
- 2:28Speaker 1: our safe house. The instinct for most self taught coders
- 2:33Speaker 1: or beginners is to just use whatever version of Python
- 2:37Speaker 1: came pre installed on their Mac or Windows machine, right,
- 2:40Speaker 1: and they just start downloading scraping packages.
- 2:42Speaker 2: Yeah, which is a huge mistake.
- 2:44Speaker 1: Why is that such a dangerous trap?
- 2:46Speaker 2: Well, using your system's default Python environment is basically playing
- 2:50Speaker 2: with fire. I mean, your computer's operating system relies heavily
- 2:53Speaker 2: on its own internal, kind of hidden version of Python
- 2:56Speaker 2: to run essential background tasks like what call of tasks,
- 2:59Speaker 2: things like a your system software or managing network settings,
- 3:02Speaker 2: running automated maintenance routines. All of those often depend on specific,
- 3:06Speaker 2: usually older versions of Python libraries.
- 3:09Speaker 1: Okay, so if I just start installing a bunch of
- 3:11Speaker 1: global scraping packages like network request libraries or HTML parsers, you.
- 3:16Speaker 2: Could accidentally overwrite or upgrade a core dependency that your
- 3:20Speaker 2: actual computer needs a function.
- 3:23Speaker 1: Oh wow, so my webscraper might start working perfectly, but
- 3:26Speaker 1: suddenly my laptop's Wi Fi manager just crashes because I
- 3:29Speaker 1: updated a background library it was relying on.
- 3:32Speaker 2: That is exactly the kind of collateral damage we want
- 3:34Speaker 2: to avoid.
- 3:35Speaker 1: So how do we avoid it? How do we build
- 3:37Speaker 1: this safehouse?
- 3:38Speaker 2: We use two critical tools. One is called PM and
- 3:41Speaker 2: the other's Pipe and V. The first tool, PM manages
- 3:44Speaker 2: the actual versions of Python.
- 3:46Speaker 1: Because Python isn't just one static language right right.
- 3:48Speaker 2: It evolves constantly. Sometimes you'll inherit a scraping project that
- 3:53Speaker 2: was built three years ago and it specifically requires Python
- 3:56Speaker 2: version three point seven point four to run properly, but
- 4:00Speaker 2: your brand new project needs version three point zero.
- 4:02Speaker 1: And PM lets you handle both exactly.
- 4:05Speaker 2: It acts like a switchboard. It allows you to install
- 4:08Speaker 2: and seamlessly toggle between multiple versions of Python the exact
- 4:11Speaker 2: same machine, and they never interact with each other.
- 4:14Speaker 1: Okay, so that handles the base language. But what about
- 4:17Speaker 1: all the specialized tools we need to bring in? You know,
- 4:19Speaker 1: the libraries that actually go out to the Internet and
- 4:22Speaker 1: read the web pages.
- 4:23Speaker 2: That is where PIPE and V comes in. It manages
- 4:26Speaker 2: the specific packages, the external code libraries by creating a secure,
- 4:31Speaker 2: completely isolated bubble we call it a virtual environment.
- 4:34Speaker 1: So it's like a quarantine zone for your code.
- 4:37Speaker 2: Yeah. Basically, when you activate a virtual environment, pidon V
- 4:40Speaker 2: temporarily rewires your computer's internal pathways. So instead of looking
- 4:45Speaker 2: in the main system hard drive for code libraries, your
- 4:48Speaker 2: project only looks inside this temporary local folder.
- 4:51Speaker 1: So I can install whatever experimental, messy packages I want
- 4:54Speaker 1: inside this bubble go wild.
- 4:56Speaker 2: It will never leak out to corrupt your main operating
- 4:58Speaker 2: system or any of your other code voting projects.
- 5:00Speaker 1: I love that. Okay, so we have our isolated safe house.
- 5:03Speaker 1: Now we need to bring in the heavy hitters, the
- 5:05Speaker 1: actual crew members who are going to pull off the job.
- 5:08Speaker 2: Right, let's meet the crew.
- 5:09Speaker 1: We install the Requests library, which handles our web traffic
- 5:12Speaker 1: reaching out to servers. We install beautiful soup four, which
- 5:16Speaker 1: we'll use to carve up the raw HTML. We install
- 5:20Speaker 1: pandas to organize our final data set, and finally we
- 5:23Speaker 1: install our command center, Jupiter Lab.
- 5:25Speaker 2: Jupiter Lab is amazing. It's the modern evolution of Jupiter notebooks,
- 5:29Speaker 2: and it fundamentally changes how you write code.
- 5:32Speaker 1: How So, like compared to just using a standard text editor.
- 5:36Speaker 2: Well, in a traditional text editor, you write, say one
- 5:38Speaker 2: hundred lines of code, and you run the whole file
- 5:40Speaker 2: at once. If it breaks online ninety nine, the whole
- 5:42Speaker 2: script fails, you have to.
- 5:43Speaker 1: Start over right, which is incredibly frustrating.
- 5:45Speaker 2: But Jupiter allows you to write and execute code in
- 5:48Speaker 2: individual blocks or sells. You can run SELL one, keep
- 5:52Speaker 2: the result in your computer's memory, and then just experiment
- 5:55Speaker 2: with SELL two.
- 5:56Speaker 1: So it's like an interactive development environment that's specifically designed
- 5:59Speaker 1: for the trial and error nature of exploring messy data.
- 6:02Speaker 2: Precisely, it's built for tinkering.
- 6:04Speaker 1: And I've heard it has this hidden feature that makes
- 6:08Speaker 1: managing all these tools much easier, something called the bang trick.
- 6:11Speaker 1: How does that fit into our workflow?
- 6:13Speaker 2: Oh? The bang trick is great. So normally, when you
- 6:16Speaker 2: hit run on a cell in Jupiter, it expects you
- 6:19Speaker 2: to be speaking Python, right right, But if you type
- 6:21Speaker 2: an exclamation point, which is called a bang in programming slang,
- 6:25Speaker 2: at the very start of the line, you are escaping
- 6:28Speaker 2: the Python interpreter.
- 6:29Speaker 1: Wait really, so what is it reading?
- 6:32Speaker 2: Instead, you're telling Jupiter to bypass Python entirely and run
- 6:36Speaker 2: that line directly in your computer's command line terminal.
- 6:40Speaker 1: Oh that's incredibly useful. So typing a bang followed by
- 6:43Speaker 1: like pip.
- 6:44Speaker 2: List, it will print out a list of every single
- 6:47Speaker 2: package currently installed in your isolated virtual environment right there
- 6:51Speaker 2: inside your notebook. You don't have to constantly toggle between
- 6:54Speaker 2: your browser window and your hidden terminal screens.
- 6:57Speaker 1: Okay, so our safe house is secure, our crew is
- 6:59Speaker 1: fully assembled inside Jupiter Lab. We are ready to venture
- 7:02Speaker 1: out in case the joint.
- 7:03Speaker 2: Time to look at the target website and figure out
- 7:05Speaker 2: where the S and P five hundred data is actually hidden.
- 7:08Speaker 1: Right, so, our request library goes out to the URL
- 7:11Speaker 1: and it downloads the raw, chaotic HTML that sits underneath
- 7:14Speaker 1: the visual website.
- 7:15Speaker 2: But there's a massive, massive strategic move that needs to
- 7:20Speaker 2: happen right at this exact moment.
- 7:22Speaker 1: Okay, what is it?
- 7:23Speaker 2: You do not just start analyzing that live connection? You
- 7:26Speaker 2: have to save a local copy of that HTML to
- 7:29Speaker 2: your own hard drive immediately.
- 7:31Speaker 1: Why I mean, isn't it easier to just pull it
- 7:33Speaker 1: fresh every time I test my code?
- 7:35Speaker 2: No, because the Internet is fundamentally unstable. Dynamic web pages
- 7:39Speaker 2: are updated, restructured, and moved all the time.
- 7:42Speaker 1: So if I'm writing a complex extraction script and it
- 7:45Speaker 1: takes me, say a few hours of trial and error,
- 7:48Speaker 1: to get my code perfectly targeted at the right data tables, and.
- 7:51Speaker 2: You're using the requests library to hit the live website.
- 7:55Speaker 2: Every single time you run a test cell in Jupiter,
- 7:58Speaker 2: you are exposing yourself to a movie targets.
- 8:00Speaker 1: Oh, I say, the site admin might push an update,
- 8:02Speaker 1: and the HTML structure I was halfway through decoding suddenly
- 8:05Speaker 1: just vanishes.
- 8:06Speaker 2: Exactly. You're left with broken code and no idea why.
- 8:08Speaker 1: To use our heist metaphor. It's like you don't stand
- 8:11Speaker 1: inside the bank lobbyes sketching the security cameras while the
- 8:14Speaker 1: guards are actively walking around looking at you.
- 8:16Speaker 2: Right, you snap a high resolution photograph, run back to
- 8:19Speaker 2: the safehouse, lock the door, and study the photograph.
- 8:22Speaker 1: I love that. Are there other risks to hitting the
- 8:25Speaker 1: live server repeatedly?
- 8:27Speaker 2: Definitely aggressively hitting a live server while you're debugging is
- 8:32Speaker 2: the absolute fastest way to get caught by those security cameras.
- 8:36Speaker 2: Web servers monitor incoming traffic.
- 8:38Speaker 1: So if a server sees my specific IP address requesting
- 8:41Speaker 1: the exact same web page like forty times in two minutes.
- 8:45Speaker 2: It knows you aren't a human reading an article. It
- 8:48Speaker 2: assumes you are either a poorly written bot or a
- 8:51Speaker 2: malicious denial of service attack. It just blocks you. Yep,
- 8:54Speaker 2: the server's automated defenses will rate limit you or completely
- 8:57Speaker 2: block your IP address.
- 8:59Speaker 1: By saving the initial raw HTML to a local file,
- 9:02Speaker 1: just using Python's basic file right mode, and then reading
- 9:05Speaker 1: that local file back into your project, you are acting ethically,
- 9:09Speaker 1: saving server bandwidth and protecting your access.
- 9:12Speaker 2: Okay, so with our local snapshots, say, Jupiter lab really
- 9:15Speaker 2: flexes its muscles as a command center. Yeah, because you
- 9:17Speaker 2: have this built in file browser right on the sidebar. Yeah,
- 9:20Speaker 2: it's super convenient. You can write click that saved HTML
- 9:23Speaker 2: file and open it in a raw text editor, or
- 9:26Speaker 2: you can use Jupiter's internal HTML viewer.
- 9:29Speaker 1: Which actually renders the page visually right, although I will
- 9:32Speaker 1: say without the external CSS styling, it looks a bit
- 9:35Speaker 1: like a bare bones website from the nineteen nineties.
- 9:38Speaker 2: True, but the underlying structure, which of what we actually
- 9:41Speaker 2: care about, is perfectly preserved.
- 9:43Speaker 1: Right. And from there you can utilize the same developer
- 9:46Speaker 1: tools you'd find in a standard Google Chrome browser, but
- 9:50Speaker 1: right inside your Jupiter workspace.
- 9:51Speaker 2: Exactly. By inspecting the rendered page, you can literally hover
- 9:55Speaker 2: your mouse over the visual table containing the financial data and.
- 9:59Speaker 1: The developer tools will automatically highlight the corresponding raw HTML
- 10:03Speaker 1: code that generates that specific visual element.
- 10:06Speaker 2: Yeah, and that's how you might discover that the data
- 10:08Speaker 2: you want is wrapped inside an HTML tag labeled with
- 10:11Speaker 2: a specific class, something like table dot wiketable.
- 10:15Speaker 1: Okay, so we found the vault. We know the goal
- 10:17Speaker 1: is sitting inside table dot wiketable. Now we transition to
- 10:20Speaker 1: the most critical phase of the heist, the heavy lifting.
- 10:23Speaker 2: We have to bypass the messy HTML security systems and
- 10:27Speaker 2: get to the pure data.
- 10:29Speaker 1: Because HTML is really just designed to tell a browser
- 10:32Speaker 1: how to display things, right. It's full of stylistic tags,
- 10:36Speaker 1: nested attributes, and formatting noise that is completely useless for
- 10:40Speaker 1: actual data analysis exactly.
- 10:42Speaker 2: So we use beautiful soup to target that specific table.
- 10:46Speaker 2: But data extraction is rarely a one shot process. It
- 10:49Speaker 2: requires iteration. We need to break the vault down piece
- 10:52Speaker 2: by piece.
- 10:53Speaker 1: Okay, what's the first piece.
- 10:54Speaker 2: First we isolate the table body, Then we drill down
- 10:57Speaker 2: to the table rows. Our immediate goal is to extract
- 11:00Speaker 2: the column headers. You know, things like company name, ticker symbol,
- 11:03Speaker 2: or market cap.
- 11:04Speaker 1: Which are usually wrapped in specific HTML header tags the
- 11:08Speaker 1: f tags.
- 11:09Speaker 2: Right, but when Beautiful Soup grabs those tags, it doesn't
- 11:11Speaker 2: just hand you clean English words. It hands you the
- 11:14Speaker 2: raw code.
- 11:15Speaker 1: Oh right, It gives you this messy string that looks
- 11:17Speaker 1: like open bracket, sheaf, class heeader, close bracket, company name,
- 11:22Speaker 1: open bracket slash, clothes bracket.
- 11:24Speaker 2: Yeah, it's a mess. Do you get our clean data?
- 11:27Speaker 2: We have to run this through a gauntlet of filters?
- 11:29Speaker 1: Okay, what's the first filter in the gauntlet.
- 11:31Speaker 2: The first filter is stripping away the HTML scaffolding. We
- 11:35Speaker 2: can loop through our list of messi tags and apply
- 11:37Speaker 2: a built in Beautiful Soup method called get text.
- 11:40Speaker 1: And what does that do?
- 11:41Speaker 2: It basically says, ignore all the structural brackets and class names,
- 11:45Speaker 2: just give me the human readable text trapped inside. We
- 11:48Speaker 2: also tell the method to use a blank space as
- 11:51Speaker 2: a separator.
- 11:52Speaker 1: Why a blank space.
- 11:53Speaker 2: To ensure that if multiple words are crammed together in
- 11:56Speaker 2: the HTML, they remain distinct when we pull them out.
- 12:00Speaker 2: Got it?
- 12:01Speaker 1: But just grabbing the text isn't enough, is it? Because
- 12:04Speaker 1: HTML is kind of notorious for having strange formatting gaps.
- 12:08Speaker 1: Like you might pull up company name, but it has
- 12:10Speaker 1: five blank spaces before and three blank spaces after it.
- 12:13Speaker 2: Yeah, those are super common, and that brings us to
- 12:15Speaker 2: the second filter. We use a standard string manipulation method
- 12:19Speaker 2: called strip dot st yep. This function scans the very
- 12:23Speaker 2: beginning and the very end of our extracted text and
- 12:26Speaker 2: aggressively shaves off any invisible, useless white space.
- 12:30Speaker 1: Okay, so that gives us crisp, clean titles. But wait,
- 12:34Speaker 1: if I'm looking at a title like company name, it
- 12:36Speaker 1: has a space right in the middle of it. And
- 12:38Speaker 1: I know from past database projects that having blank spaces
- 12:41Speaker 1: in your column headers is just a massive disaster waiting
- 12:44Speaker 1: to happen.
- 12:44Speaker 2: Oh. Absolutely, databases, sql queries, even basic CSV files handle
- 12:50Speaker 2: blank spaces very poorly, right.
- 12:52Speaker 1: Because the system might read company name as two completely
- 12:55Speaker 1: separate commands rather than one single identifier.
- 12:59Speaker 2: Exactly. It is standard data engineering practice to future proof
- 13:03Speaker 2: your data sets by replacing all internal spaces with underscores.
- 13:07Speaker 1: So our third filter uses a simple replacement function to
- 13:10Speaker 1: turn company name with a space into company underscore name.
- 13:14Speaker 2: You got it. We've stripped got HTML, shave the edges,
- 13:16Speaker 2: and secured the spaces.
- 13:18Speaker 1: But there is a final security layer protecting this vault,
- 13:21Speaker 1: isn't there?
- 13:21Speaker 2: There always is?
- 13:22Speaker 1: If you've ever read a dense Wikipedia article or financial report.
- 13:26Speaker 1: You know, they are full of those tiny bracketed footnote citations,
- 13:30Speaker 1: little letters or numbers like bracket be bracket or bracket
- 13:33Speaker 1: one bracket sitting right next to the text.
- 13:35Speaker 2: Oh yeah, those are a nightmare for scraping, because if
- 13:38Speaker 2: we run our code right now, our cleanheader becomes company
- 13:42Speaker 2: name bracket bracket.
- 13:43Speaker 1: Which completely ruins our automated column mapping later on right.
- 13:46Speaker 2: We need a laser scalpel to cut those out.
- 13:48Speaker 1: And this is where things get intense. To remove those
- 13:51Speaker 1: unpredictable citations, we have to deploy Python's remodule, which introduces
- 13:56Speaker 1: regular expressions commonly known as rejects lejecs. Now, I have
- 14:00Speaker 1: to admit rejects is usually where a lot of people
- 14:03Speaker 1: just close their laptops and walk away.
- 14:04Speaker 2: It does have that reputation.
- 14:06Speaker 1: I mean, when you look at rejec syntax, it looks
- 14:08Speaker 1: incredibly intimidating. It looks like a string of random backslashes,
- 14:12Speaker 1: brackets and letters that make absolutely zero phonetic sense. Is
- 14:16Speaker 1: it really necessary or is there a simpler way to
- 14:19Speaker 1: just use a basic find and replace command.
- 14:22Speaker 2: It is a totally common reaction to be intimidated by it.
- 14:25Speaker 2: But Rejects is a non negotiable skill for serious data extraction.
- 14:29Speaker 1: Why won't basic find and replace work.
- 14:32Speaker 2: Because a basic find and replace command only works if
- 14:36Speaker 2: you know the exact literal word you are looking for.
- 14:40Speaker 2: If you know the footnote is always the letter B.
- 14:43Speaker 2: Sure you can write a simple rule to delete B.
- 14:45Speaker 1: But in the real world, you don't know if the
- 14:47Speaker 1: next page will have a footnote of AC or a
- 14:50Speaker 1: completely random number exactly.
- 14:52Speaker 2: You don't know the exact character, but you do know
- 14:54Speaker 2: the pattern. Ah okay, you know it will always be
- 14:57Speaker 2: an opening bracket followed by some alphanumeric character followed by
- 15:00Speaker 2: a closing bracket. Rejects is an engine specifically designed for
- 15:04Speaker 2: pattern matching.
- 15:05Speaker 1: So instead of telling the computer what we'd to look for,
- 15:08Speaker 1: we are teaching the computer what shape the trap looks like.
- 15:11Speaker 2: That is exactly the right way to think about it,
- 15:13Speaker 2: and to build that shape in Rejects, we just string
- 15:16Speaker 2: together specific symbols.
- 15:17Speaker 1: Let's walk through building that shape. We have our clean header,
- 15:21Speaker 1: but there's an underscore, an open bracket, a letter, and
- 15:23Speaker 1: a close bracket attached to the end. How do we
- 15:26Speaker 1: target that?
- 15:27Speaker 2: First, we use a backslash combined with an underscore that
- 15:30Speaker 2: tells the engine to look for a literal underscore character.
- 15:33Speaker 1: Okay, backslash underscore.
- 15:35Speaker 2: Got it. Then we use another backslash before an opening bracket.
- 15:39Speaker 1: Wait, why do we need a backslash for the bracket
- 15:42Speaker 1: because we have to escape it.
- 15:44Speaker 2: In the Rejects language, brackets normally act as functional commands.
- 15:48Speaker 2: They do things by escaping it with a backslash. We
- 15:51Speaker 2: are telling the engine stop reading this as code. I
- 15:54Speaker 2: literally just want you to find the shape of an
- 15:56Speaker 2: open bracket.
- 15:57Speaker 1: Okay, that makes sense. So we have our underscore and
- 16:00Speaker 1: our open bracket. How do we tell it to find
- 16:03Speaker 1: literally any letter or number that might be hiding inside
- 16:06Speaker 1: the bracket.
- 16:07Speaker 2: We use a special wildcard command. It's usually represented by
- 16:10Speaker 2: a backslash and a lowercase W, which stands for any
- 16:13Speaker 2: word character.
- 16:14Speaker 1: So backslash W covers ab one, two, whatever is in there, yep.
- 16:19Speaker 2: And finally we escape a closing bracket with one last backslash.
- 16:23Speaker 1: So the whole pattern is backslash underscore, backslash open bracket,
- 16:26Speaker 1: backslash W, backslash closed bracket.
- 16:28Speaker 2: Exactly, we tell our Python script to scan our text
- 16:32Speaker 2: find anything that matches that exact structural pattern and substitute
- 16:35Speaker 2: it with absolute.
- 16:36Speaker 1: Nothingness, a blank string.
- 16:38Speaker 2: Right in a fraction of a second, the Rajex engine
- 16:41Speaker 2: sweeps through the data and vaporizes every single unpredictable citation bracket.
- 16:47Speaker 1: That is incredibly satisfying. So our column headers are finally
- 16:50Speaker 1: pristine and database ready. We've bypassed the security. Now we
- 16:55Speaker 1: need to actually load the price the data rows into
- 16:58Speaker 1: the getaway car.
- 16:59Speaker 2: We shift our focus back to the beautiful soup object
- 17:02Speaker 2: and target the table rows the trayas tags. But we
- 17:05Speaker 2: have to apply a really crucial piece of logic here.
- 17:07Speaker 2: What's that if we just grab every single row indiscriminately,
- 17:10Speaker 2: we will accidentally pull in the messy, unformatted head row
- 17:14Speaker 2: we just spent four filters.
- 17:15Speaker 1: Avoiding, oh right, because the headers are technically the first
- 17:18Speaker 1: row of the table exactly.
- 17:19Speaker 2: So we use an index check in our loop to
- 17:21Speaker 2: basically say, if this is the very first row in
- 17:23Speaker 2: the sequence, skip it entirely.
- 17:25Speaker 1: We only want the core financial data, so we skip
- 17:27Speaker 1: index zero and we loop through the data cells the
- 17:30Speaker 1: tea DAGs in those remaining rows, extracting the raw text.
- 17:33Speaker 2: And here we absolutely must deploy our strip filter again,
- 17:37Speaker 2: because raw HTML data cells are infamous for hiding invisible
- 17:41Speaker 2: new line characters.
- 17:42Speaker 1: New line characters like when you hit enter on your keyboard.
- 17:45Speaker 2: Yeah, when web developers build sites, they frequently hit the
- 17:49Speaker 2: enter or return key to create line breaks in their
- 17:53Speaker 2: HTML code, simply to make it easier for their own
- 17:55Speaker 2: human eyes to read the raw files.
- 17:58Speaker 1: But a web browser just ignores those lines breaks when
- 18:00Speaker 1: painting the visual website on your screen, right right.
- 18:02Speaker 2: But Python reads everything literally. It interprets those developer keystrokes
- 18:07Speaker 2: as actual data represented by the end character.
- 18:10Speaker 1: So if you don't aggressively strip those out, your final
- 18:13Speaker 1: spreadsheet will be riddled with bizarre, random line breaks that
- 18:16Speaker 1: completely shatter your formatting. Okay, so we are looping through
- 18:20Speaker 1: the rows, dodging the headers, stripping the invisible line breaks,
- 18:24Speaker 1: and grabbing the pure text. Now we have to package
- 18:27Speaker 1: the loop. We need to organize this stream of data
- 18:30Speaker 1: into a specific architectural format that advanced data science tools
- 18:34Speaker 1: actually require.
- 18:35Speaker 2: Right in Python, this specific format is called a list
- 18:38Speaker 2: of lists.
- 18:39Speaker 1: A list of lists, how should we visualize that?
- 18:42Speaker 2: Visually? You can imagine a list of lists as a
- 18:45Speaker 2: massive master shipping container that outer conpata represents the entire
- 18:50Speaker 2: table you just scraped.
- 18:51Speaker 1: Okay, the big box.
- 18:52Speaker 2: Inside that master container, you place a series of smaller
- 18:55Speaker 2: individual boxes. Each of those inner boxes holds the distinct
- 19:00Speaker 2: data points for one single row from your table.
- 19:03Speaker 1: Ah, I see. So it creates a multi dimensional grid
- 19:06Speaker 1: that strictly preserves the relationship between the columns and the
- 19:09Speaker 1: rows exactly.
- 19:10Speaker 2: It keeps everything perfectly organized.
- 19:12Speaker 1: So we have our clean list of column headers, and
- 19:14Speaker 1: we have our master shipping container holding all the perfectly
- 19:17Speaker 1: formatted data rows. And that brings us to the absolute
- 19:21Speaker 1: climax of the heist.
- 19:22Speaker 2: We take those two structured lists and we feed them
- 19:25Speaker 2: directly into pandas.
- 19:26Speaker 1: And for those who don't know, pandas is arguably the
- 19:29Speaker 1: most powerful data manipulation library in the entire Python ecosystem.
- 19:33Speaker 2: It really is. It takes your raw lists and instantly
- 19:37Speaker 2: renders them into a data frame.
- 19:38Speaker 1: How does a data frame differ from just, you know,
- 19:41Speaker 1: pasting the data into an Excel spreadsheet.
- 19:43Speaker 2: Well, an Excel spreadsheet is a static visual grid, and
- 19:47Speaker 2: if you load it with a million rows of data,
- 19:49Speaker 2: it starts to choke and crash.
- 19:51Speaker 1: Yeah, anyone who's used to Excel knows that pain.
- 19:53Speaker 2: But a panda's data frame is an optimized, vectorized data
- 19:57Speaker 2: structure held directly in your computer's active memory. It can
- 20:01Speaker 2: handle millions of rows effortlessly. Wow. And once your data
- 20:05Speaker 2: is in a data frame, it is primed for high
- 20:07Speaker 2: level operations. You can instantly export it to a clean CSV,
- 20:11Speaker 2: you can push it directly into a live SQL database,
- 20:14Speaker 2: or you can even feed it straight into a machine
- 20:16Speaker 2: learning algorithm.
- 20:17Speaker 1: It's the payoff moment. It's where all the meticulous setup
- 20:21Speaker 1: transforms web scraping from a frustrating chore into an absolute superpower.
- 20:26Speaker 1: I mean, you have literally imposed rigid order onto the chaotic,
- 20:29Speaker 1: messy reality of the Internet.
- 20:31Speaker 2: You've taken unstructured noise and built a reliable, automated pipeline
- 20:34Speaker 2: for actionable intelligence.
- 20:36Speaker 1: We have covered a massive amount of conceptual ground today,
- 20:39Speaker 1: moving from isolated virtual environments all the way down to
- 20:42Speaker 1: the microscopic level of rejex pattern matching. It's a lot
- 20:45Speaker 1: to take in, it is, so before we wrap up,
- 20:47Speaker 1: let's step back and look at the goal that we build.
- 20:50Speaker 1: Let's do a quick mental review exercise. Sounds good if
- 20:53Speaker 1: you find yourself staring at a wall of extracted text
- 20:56Speaker 1: that is plagued by invisible formatting gaps, database breaking spaces,
- 21:00Speaker 1: and random Wikipedia style footnotes. You really just need to
- 21:03Speaker 1: deploy a three stage filter. What are those three stages?
- 21:06Speaker 2: Okay? First, you execute the strip method to aggressively shave
- 21:10Speaker 2: off the invisible newline characters in edge spaces. Second, you
- 21:14Speaker 2: run a standard string replacement to swap out the vulnerable
- 21:17Speaker 2: blank spaces with structurally sound.
- 21:19Speaker 1: Underscores and the final boss.
- 21:22Speaker 2: Finally, you deploy the Redjex engine as your laser scalpel
- 21:25Speaker 2: to define a structural pattern and excise those unpredictable citation brackets.
- 21:29Speaker 1: Perfect. If you understand the logic behind those three filters,
- 21:32Speaker 1: you are officially a dangerous data gatherer. Let's review the
- 21:36Speaker 1: blueprints of the entire heist one last time.
- 21:38Speaker 2: Let's good.
- 21:39Speaker 1: We didn't just rush the bank. We build a secure safehouse,
- 21:43Speaker 1: using PM to manage our base language and pipe in
- 21:46Speaker 1: sieve to isolate our dependencies so we wouldn't break our
- 21:49Speaker 1: operating system.
- 21:50Speaker 2: We loaded our crew into Jupiter Lab, utilizing the Bang
- 21:54Speaker 2: trick to control our terminal right from the notebook.
- 21:57Speaker 1: We used requests to pull the target instantly, saving a
- 22:00Speaker 1: local snapshot. To avoid rate limiting and protect our server access.
- 22:04Speaker 1: We used Chrome developer tools right inside our notebook to
- 22:07Speaker 1: map the HTML vault.
- 22:08Speaker 2: We iterated through the table logic, applying our three stage
- 22:12Speaker 2: filter to clean the.
- 22:12Speaker 1: Headers, and finally we extracted the rose, packed them into
- 22:16Speaker 1: a multi dimensional list of lists, and made our getaway
- 22:19Speaker 1: by dropping the pristine data into a powerful panda's data frame.
- 22:23Speaker 2: It is a highly repeatable, professional grade workflow that respects
- 22:28Speaker 2: the underlying systems while extracting maximum value.
- 22:31Speaker 1: At the beginning of this deep dive, I painted a
- 22:33Speaker 1: picture of the manual, soul crushing labor of copying and
- 22:37Speaker 1: pasting data from the web. You now understand the mechanisms
- 22:40Speaker 1: and the mindset required to build a digital robot that
- 22:42Speaker 1: does that exact work for you.
- 22:44Speaker 2: You never have to manually clean a table again.
- 22:46Speaker 1: But I want to leave you with a final thought
- 22:48Speaker 1: to ponder. Now that you have the power to turn
- 22:50Speaker 1: the chaotic Internet into structured, analyzable data, what unseen realities
- 22:55Speaker 1: could you uncover?
- 22:56Speaker 2: That's the real question.
- 22:57Speaker 1: Imagine if you set your scraper to run automatically, quietly
- 23:02Speaker 1: pulling public data from a specific municipal page, or a
- 23:05Speaker 1: financial blog or a real estate listing site, every single
- 23:08Speaker 1: day for an entire year.
- 23:10Speaker 2: Just compiling it day after day.
- 23:12Speaker 1: What long term trends would emerge that literally no one
- 23:15Speaker 1: else can see simply because they don't have the patience
- 23:17Speaker 1: to manually track it. What massive hidden data set is
- 23:21Speaker 1: out there in the wild right now, just waiting for
- 23:23Speaker 1: you to build the right robot to unlock it. Go
- 23:26Speaker 1: build your safehouse, inspect the code, and see what you
- 23:28Speaker 1: can find.