Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 6: From Scrapy Framework Foundations to Professional Spiders
Transcript
- 0:00Speaker 1: Imagine, just for a second, a total nightmare scenario. Oh boy, Right,
- 0:05Speaker 1: so you need to gather data from thousands of web pages.
- 0:10Speaker 1: Maybe you want to track use vehicle prices across the
- 0:12Speaker 1: entire country to find a really good deal.
- 0:14Speaker 2: Or maybe you're gathering a massive data set for an academic.
- 0:17Speaker 1: Project exactly, or like trying to monitor a rapidly shifting
- 0:22Speaker 1: financial market. And if you attempt to do this manually,
- 0:25Speaker 1: like opening a browser tab, highlighting the text, copy and
- 0:28Speaker 1: pasting it into some massive Excel spreadsheet and then doing
- 0:31Speaker 1: it again, I mean it is practically impossible.
- 0:34Speaker 2: Yeah, you would be sitting at your keyboard for literally years.
- 0:37Speaker 1: Right. But let's say you know a little bit of Python,
- 0:39Speaker 1: you understand basic scripting. The real question is how do
- 0:43Speaker 1: you scale up from a simple script that reads one
- 0:45Speaker 1: page into a full fledged automated data gathering machine.
- 0:50Speaker 2: Well, scaling is where most people hit a brick wall.
- 0:53Speaker 2: Like reading a single web page with a few lines
- 0:55Speaker 2: of code is a really fun weekend project. But doing
- 0:58Speaker 2: it across thousands of pages reliably without your computer crashing
- 1:03Speaker 2: and without getting blocked by the websites, that requires a
- 1:05Speaker 2: completely different approach. You have to graduate to a robust system.
- 1:09Speaker 1: And to answer that, we are doing a deep dive
- 1:12Speaker 1: today into the ultimate guide to building a web scraper
- 1:15Speaker 1: from the ground up using Python. Yes, we're going to
- 1:18Speaker 1: explore a very specific, highly popular framework called Scrapeye. We'll
- 1:23Speaker 1: walk step by step through how it works under the hood,
- 1:26Speaker 1: how to navigate the incredibly messy reality of real world
- 1:30Speaker 1: Internet data, and most importantly, how to avoid the common
- 1:35Speaker 1: traps that will bring your code to a grinding halt.
- 1:38Speaker 2: It's a minefield out there, it really is.
- 1:40Speaker 1: So we're going to use a car listing website as
- 1:41Speaker 1: our practical sandbox today. But first, what actually is scraping?
- 1:45Speaker 2: So web scraping, when you strip away all the technical jargon,
- 1:49Speaker 2: is essentially just writing code to do what your browser
- 1:51Speaker 2: already does. Okay, it requests a web page, reads the
- 1:54Speaker 2: underlying code, extracts the specific pieces of information you actually
- 1:58Speaker 2: care about, and then saves them. Moving to Scrapey means
- 2:01Speaker 2: stepping into the big leagues of this process. But before
- 2:05Speaker 2: we even get into the syntax, we have to talk
- 2:07Speaker 2: about a fundamental shift in how you think about writing code.
- 2:11Speaker 1: Let's focus on that mindset shift, because there is a
- 2:15Speaker 1: very clear line drawn between a library and a framework.
- 2:18Speaker 1: And I know a lot of people might have used
- 2:20Speaker 1: like Python libraries like requests or beautiful Soup in the
- 2:23Speaker 1: past to scrape a few pages. So what is the
- 2:27Speaker 1: functional difference between those and Scrapey.
- 2:29Speaker 2: Think about the control flow of your program. A library
- 2:33Speaker 2: like requests is basically just a collection of related helpful functions.
- 2:36Speaker 2: You write your script from top to bottom, line one,
- 2:39Speaker 2: line two, line three. When your code needs to fetch
- 2:42Speaker 2: a web page, it reaches out to the request library,
- 2:45Speaker 2: uses the function, and gets the result.
- 2:46Speaker 1: So my script is the boss exactly.
- 2:48Speaker 2: Your script is the boss. It makes all the decisions
- 2:51Speaker 2: about when and where things happen. But a framework like
- 2:53Speaker 2: Scrapey flips that entirely. It provides a complete, pre built
- 2:58Speaker 2: application architecture. You aren't calling scrapey whenever you feel like it.
- 3:02Speaker 2: Scrapeye is running the show and you are just plugging
- 3:05Speaker 2: your specific instructions into its pre defined slots.
- 3:09Speaker 1: Okay, wait, so using a library sounds kind of like
- 3:12Speaker 1: going to a hardware store, Like you buy a hammer,
- 3:15Speaker 1: some nails, some two by fours and you go home
- 3:17Speaker 1: and build your house in your backyard however you want, yes, exactly,
- 3:21Speaker 1: But using a framework sounds more like walking onto a
- 3:24Speaker 1: fully managed, active commercial construction site.
- 3:27Speaker 2: That's a great way to put it.
- 3:28Speaker 1: Like. You don't just start hammering away at random. You
- 3:30Speaker 1: have to put your materials exactly where the site forman
- 3:32Speaker 1: tells you to put them, and you work on their schedule.
- 3:34Speaker 2: The Foreman analogy is incredibly accurate. In computer science, this
- 3:39Speaker 2: architectural concept is actually known as inversion of control.
- 3:43Speaker 1: Inversion of control.
- 3:44Speaker 2: Right, the framework dictates the flow of the program. It
- 3:48Speaker 2: handles all the heavy lifting things like network management, scheduling requests,
- 3:53Speaker 2: handling errors, and it simply asks your code for instructions
- 3:57Speaker 2: when it needs them.
- 3:58Speaker 1: Wow.
- 3:58Speaker 2: Okay, there's actually a classic forming joke about this called
- 4:01Speaker 2: the Hollywood principle. Oh yeah, it goes, don't call us,
- 4:04Speaker 2: we'll call you.
- 4:05Speaker 1: Don't call us, we'll call you. Okay. So with Scrapie,
- 4:08Speaker 1: we aren't writing a traditional top to bottom script. We
- 4:11Speaker 1: are writing small chunks of logic, putting them in the
- 4:14Speaker 1: right folders, and waiting for the framework's engine to call
- 4:17Speaker 1: upon them exactly.
- 4:18Speaker 2: And because Scrapie handles all that complex background infrastructure, you
- 4:23Speaker 2: don't have to reinvent the wheel. You don't have to
- 4:25Speaker 2: write code to manage what happens if say a network
- 4:29Speaker 2: connection drops mid download, or how to handle multiple page
- 4:33Speaker 2: requests simultaneously.
- 4:34Speaker 1: The foreman takes care of the logistics.
- 4:36Speaker 2: Yep, you just focus on what data you want to extract.
- 4:39Speaker 1: Okay. But if the foreman is in charge, how do
- 4:42Speaker 1: we actually interact with this framework to get a project
- 4:45Speaker 1: off the ground. Yeah, I mean we can't just open
- 4:47Speaker 1: a blank text file and start typing.
- 4:49Speaker 2: No, definitely not. Everything begins with the command line interface
- 4:54Speaker 2: or CLI. First, you set up a local Python virtual
- 4:58Speaker 2: environment to keep your dependencies clean. Then you install scrapey
- 5:02Speaker 2: and open your terminal. From there, you type a really
- 5:05Speaker 2: simple command scrapey start project, followed by whatever you want
- 5:08Speaker 2: to name it.
- 5:09Speaker 1: Okay, So, for our car website example, let's say we're
- 5:12Speaker 1: targeting truecar dot com, so we'll just name the project
- 5:14Speaker 1: true perfect.
- 5:15Speaker 2: So you hit enter on that and Scrapeye's engine wakes
- 5:18Speaker 2: up and automatically generates a complete directory structure right there
- 5:22Speaker 2: on your computer.
- 5:23Speaker 1: Oh wow, it builds it all out, it does.
- 5:25Speaker 2: You get a configuration file, various settings files, and a
- 5:29Speaker 2: dedicated folder called spiders. And spiders in scrapy terminology are
- 5:34Speaker 2: the actual custom scripts that do the crawling.
- 5:37Speaker 1: So it builds the entire scaffolding for you.
- 5:39Speaker 2: It does. But, and this is important, you shouldn't rush
- 5:42Speaker 2: into that spider's folder to write your final script right away.
- 5:44Speaker 1: Really? Why not?
- 5:45Speaker 2: Well, scrapeye provides an incredibly valuable interactive sandbox called the
- 5:49Speaker 2: scrapy Shell. You launch it from the command line by
- 5:52Speaker 2: typing scrapey shell, and then the url you want to target,
- 5:55Speaker 2: ye goes out, fetches the page and lets you test
- 5:58Speaker 2: your extraction commands right there, terminal before you write any
- 6:01Speaker 2: real code.
- 6:02Speaker 1: Wait, I have to stop you there. If I know
- 6:04Speaker 1: what website I want to scrape, and I know I
- 6:06Speaker 1: want the car prices, why do I need to play
- 6:08Speaker 1: around in a sandbox environment first? Like? Why not just
- 6:11Speaker 1: write the final code, hit run and see if it works.
- 6:14Speaker 2: That brings up a very harsh reality of the Internet.
- 6:17Speaker 2: Websites fight back. Oh yeah, they are designed for human
- 6:21Speaker 2: beings using web browsers, not automated bots slipping up their
- 6:26Speaker 2: data at lightning speed.
- 6:27Speaker 1: Makes sense.
- 6:28Speaker 2: So if you take your brand new scrapey setup and
- 6:30Speaker 2: point it directly at a commercial site like Truecar, there
- 6:34Speaker 2: is a very high probability the server will look at
- 6:36Speaker 2: your request, deny it, and return a four h three forbidden.
- 6:40Speaker 1: Error, a flat out rejection. But how does the server
- 6:42Speaker 1: instantly know we're a bot?
- 6:44Speaker 2: It comes down to something called a user agent's strength. Okay,
- 6:47Speaker 2: whenever you visit a website, whether it's on your phone
- 6:49Speaker 2: or your laptop, your browser sends a hidden text header
- 6:52Speaker 2: as part of the initial handshake with the server. It
- 6:55Speaker 2: acts like an ID badge. A normal one might look
- 6:58Speaker 2: like a long string of text saying you know Mozilla
- 7:01Speaker 2: five point zero, Windows NT ten point zero, Apple WebKit,
- 7:05Speaker 2: Chrome and so on. It tells the server, Hi, I
- 7:08Speaker 2: am a regular Chrome browser running on a Windows computer.
- 7:12Speaker 1: Let me guess Screepy's default ID badge is a little
- 7:15Speaker 1: more honest.
- 7:15Speaker 2: Oh, it's very honest. Out of the box, Scrapy's user
- 7:19Speaker 2: agent literally announces itself. It says something like Scrapy two
- 7:23Speaker 2: point five point zero.
- 7:25Speaker 1: Wow.
- 7:25Speaker 2: Yeah. You are essentially walking up to the bouncer at
- 7:28Speaker 2: an exclusive club wearing a bright Neon T shirt that
- 7:31Speaker 2: says I am an automated robot here to take all
- 7:34Speaker 2: your data, and the.
- 7:35Speaker 1: Server security software sees that and immediately drops the four
- 7:39Speaker 1: h three forbidden error on you.
- 7:40Speaker 2: Exactly a bounce, So.
- 7:42Speaker 1: We need a fake ID. How do we get past
- 7:44Speaker 1: the server security without rewriting the core framework.
- 7:47Speaker 2: It is surprisingly simple. Actually, you can specify a custom
- 7:50Speaker 2: user agent string right inside the scrapy shell command and
- 7:53Speaker 2: later in your project's settings. Oh really, yeah, you just
- 7:56Speaker 2: grab a user agent string from a standard browser. You
- 7:58Speaker 2: can easily find lists of them online and pass it
- 8:00Speaker 2: along with your requests.
- 8:01Speaker 1: That's it. Yep.
- 8:03Speaker 2: Suddenly the website server looks at your request, sees what
- 8:07Speaker 2: looks like a standard Firefox or Safari browser, assumes you're
- 8:10Speaker 2: a normal human browsing for a car, and just lets
- 8:13Speaker 2: you in.
- 8:14Speaker 1: That is incredibly clever. So we bypass the bouncer using
- 8:18Speaker 1: the custom user agent in the scrapy shell, and once
- 8:22Speaker 1: we are the shell gives us a response object, which
- 8:25Speaker 1: is basically the downloaded web page, so we can test
- 8:28Speaker 1: different commands right there to see if we can isolate
- 8:30Speaker 1: the car prices. If the command works in the sandbox,
- 8:33Speaker 1: we know it's safe to put into our final code.
- 8:35Speaker 2: Exactly because troubleshooting data extraction is like ninety percent of
- 8:39Speaker 2: the work in webscraping, I believe it. The shell allows
- 8:42Speaker 2: you to fail fast and fix your logic in real time,
- 8:45Speaker 2: which is way better than running a massive script, waiting
- 8:48Speaker 2: five minutes and then realizing you made a typo in
- 8:50Speaker 2: your search parameters.
- 8:51Speaker 1: Yeah, that would be incredibly frustrating. So let's move from
- 8:54Speaker 1: the sandbox to the actual engine. We've tested our commands,
- 8:57Speaker 1: it's time to build the spider.
- 8:58Speaker 2: Itself, right, So we navigate into that spider's directory scrapey
- 9:03Speaker 2: created for us, and we create a new Python file.
- 9:06Speaker 2: Let's call it true spider dot pie.
- 9:09Speaker 1: Okay, Now, what usually shocks people when they see a
- 9:12Speaker 1: well written scrapey Spider is how minimal it is. You
- 9:15Speaker 1: have the power of this massive framework behind you, but
- 9:19Speaker 1: the final script you write might only be twenty or
- 9:22Speaker 1: thirty lines of code.
- 9:24Speaker 2: That's wild. Let's break down the anatomy of those lines. So,
- 9:27Speaker 2: assuming someone is like visualizing this in their code editor
- 9:30Speaker 2: right now, Sure you import scrape and then you have
- 9:33Speaker 2: to create a Python class. The convention is to name
- 9:36Speaker 2: it after the domain, so true car spider yep. But
- 9:39Speaker 2: the crucial part, time back to the Hollywood principle, is
- 9:42Speaker 2: that this class inherits from scrapy spider. For someone who
- 9:46Speaker 2: only knows basic Python scripting, what does it mean to
- 9:49Speaker 2: inherit from a base class.
- 9:51Speaker 1: Think of it like buying a vehicle chassis. You don't
- 9:53Speaker 1: want to forge your own steel and build an axle
- 9:55Speaker 1: from scratch trick, right. You buy a pre built chassis
- 9:58Speaker 1: that already has the wheels, this steering column, and the
- 10:01Speaker 1: transmission attached in Python. When your new class inherits from
- 10:05Speaker 1: scrapey Spider, you are taking Scrapey's pre built scraping chassis.
- 10:09Speaker 1: It already knows how to make web requests, handle cookies,
- 10:12Speaker 1: follow redirects, all of that.
- 10:14Speaker 2: You are just painting the chassis and adding your specific
- 10:17Speaker 2: engine tuning. The framework recognizes your class as a legitimate
- 10:21Speaker 2: spider because it shares that foundational DNA.
- 10:24Speaker 1: So we have our inherited chassis now inside this class.
- 10:28Speaker 1: Scrapeye expects us to define two very specific methods. Yes.
- 10:32Speaker 1: The first is start requests. This seems pretty straightforward. Was
- 10:36Speaker 1: where we provide a list of the URLs we want
- 10:38Speaker 1: to scrape and tell the framework to go fetch them.
- 10:41Speaker 1: And the second method is parse. Once Scrapy downloads the HTML,
- 10:45Speaker 1: it feeds that raw data into the parse method, which
- 10:48Speaker 1: acts as our callback. That's where we extract the gold.
- 10:51Speaker 2: Exactly start requests and parse are the beating heart of
- 10:54Speaker 2: the spider. But inside start requests there is a specific
- 10:58Speaker 2: Python keyword used that often t beginners up.
- 11:01Speaker 1: What's that?
- 11:01Speaker 2: Instead of a standard return statement, we use the yield statement.
- 11:04Speaker 1: Ah, yield.
- 11:05Speaker 2: Yeah, so the code looks like, you know, yields crapy
- 11:08Speaker 2: request yield is?
- 11:09Speaker 1: I mean, that's a strange concept. Wrap your head around
- 11:11Speaker 1: at first. Why are we yielding instead of just returning
- 11:13Speaker 1: of value? Are we yielding the right of way to
- 11:16Speaker 1: the computer's processor or something in a way.
- 11:18Speaker 2: You actually are. We need to talk about how computers
- 11:21Speaker 2: handle time delays. Okay, if you write a standard synchronous
- 11:25Speaker 2: script that runs from top to bottom, what happens when
- 11:28Speaker 2: it asks a server for a web page?
- 11:30Speaker 1: Well, the computer has to wait for the server to reply.
- 11:33Speaker 1: It takes time for the data to travel across the internet.
- 11:36Speaker 2: Exactly, and during that time, your script is completely frozen.
- 11:41Speaker 2: The processor is just sitting there idling, twiddling its thumbs
- 11:45Speaker 2: until the data arrives.
- 11:46Speaker 1: Right now.
- 11:47Speaker 2: If you are requesting one page, a half second delay
- 11:49Speaker 2: doesn't matter. But if you are requesting ten thousand pages,
- 11:53Speaker 2: those half seconds add up to hours of wasted time.
- 11:55Speaker 1: Oh wow, Yeah, so yield.
- 11:57Speaker 2: Solves this by introducing a synchronous behavior.
- 12:00Speaker 1: How does yield actually change the behavior of the program though.
- 12:03Speaker 2: Think of it as a lazy return, or like a
- 12:06Speaker 2: pause button. Okay, When Python hits yield scrapey request, it
- 12:11Speaker 2: fires off the requests to the Internet, But instead of
- 12:13Speaker 2: freezing and waiting for the response, the function pauses its
- 12:16Speaker 2: own execution and hands control back to Scrapeye's main engine.
- 12:19Speaker 2: Oh I see, it says, Hey, I'm waiting on this,
- 12:21Speaker 2: Go do something else in the meantime. So scrapey can
- 12:25Speaker 2: then fire off dozens of other requests concurrently. Right when
- 12:29Speaker 2: the first web page finally downloads, the engine wakes that
- 12:32Speaker 2: original function back up, hands it the data, and it
- 12:35Speaker 2: continues exactly where it left off.
- 12:38Speaker 1: So we aren't just sitting in line waiting for one
- 12:40Speaker 1: transaction to finish before starting the next. It's a continuous
- 12:43Speaker 1: flowing stream exactly I imagined. That has huge implications for
- 12:47Speaker 1: memory management as.
- 12:48Speaker 2: Well, massive implications. If you try to download ten thousand
- 12:52Speaker 2: web pages using standard logic, you might try to hold
- 12:56Speaker 2: all of them in your computer's ram at once.
- 12:57Speaker 1: Before processing them, which would be bad.
- 13:00Speaker 2: Yeah, your machine would quickly run out of memory and crash.
- 13:03Speaker 2: By using yield as a generator, scrape processes one piece
- 13:06Speaker 2: of data outputs, it freeze up the memory and then
- 13:09Speaker 2: moves on to the next. That is so smart it
- 13:12Speaker 2: creates an incredibly lightweight, memory efficient stream. You could scrape
- 13:16Speaker 2: millions of pages on a standard laptop without it even
- 13:20Speaker 2: breaking a sweat.
- 13:21Speaker 1: Okay, so the spider is asynchronously requesting pages, yielding control
- 13:26Speaker 1: and receiving the HTML back. Now we face the reality
- 13:29Speaker 1: of the Internet.
- 13:30Speaker 2: The messy part.
- 13:31Speaker 1: Yeah, we have this gigantic wall of MESSYHTML code, and
- 13:34Speaker 1: we need to find the specific data points we want,
- 13:37Speaker 1: like the car model and the price.
- 13:39Speaker 2: Right. So this all happens in the parse method. Let's
- 13:41Speaker 2: look at our car website example again. Okay, a standard
- 13:44Speaker 2: search results page lists about thirty cars. If you look
- 13:47Speaker 2: at the raw HTML, it's just a sea of brackets
- 13:50Speaker 2: and tags. We need a way to pinpoint the exact
- 13:54Speaker 2: locations of those cars. To do this, we use something
- 13:57Speaker 2: called an XPath.
- 13:58Speaker 1: Selector an xpath's how does that work?
- 14:01Speaker 2: So? XPath is basically a query language designed specifically for
- 14:05Speaker 2: navigating through the tree like structure of an HTML or
- 14:08Speaker 2: XML document. Okay, think of it. Like providing exact GPS
- 14:12Speaker 2: coordinates for the data you want.
- 14:13Speaker 1: I like that analogy.
- 14:14Speaker 2: Yeah, by using the scrape sheell. Earlier, we investigated the
- 14:18Speaker 2: web pages structure and discovered that every single car on
- 14:20Speaker 2: the page is contained within a specific HTML dift block
- 14:24Speaker 2: that has a custom attribute attached to it.
- 14:26Speaker 1: What was the attribute?
- 14:27Speaker 2: It was something like data class equals listings.
- 14:30Speaker 1: Uh. Okay, So we can use XPath to tell scrape, hey,
- 14:34Speaker 1: ignore the entire web page. Just follow these GPS coordinates,
- 14:37Speaker 1: find every divtag that has data qui equals listings attext
- 14:40Speaker 1: to it and.
- 14:41Speaker 2: Grabble exactly, and scrape will instantly pull all thirty of
- 14:45Speaker 2: those car blocks into a Python list.
- 14:47Speaker 1: Wow.
- 14:47Speaker 2: From there, the programming is really simple. We write a
- 14:50Speaker 2: standard for loop. We iterate through those thirty blocks one
- 14:53Speaker 2: by one. Inside the loop, we use more specific XPath
- 14:56Speaker 2: coordinates to dig into the block and extract the URL
- 14:59Speaker 2: to the specific car, the model, name, the mileage, and
- 15:02Speaker 2: the price. We take those four pieces of text and
- 15:05Speaker 2: store them into a Python dictionary.
- 15:07Speaker 1: And dictionaries are incredibly important here because Screepy's data pipeline
- 15:12Speaker 1: is built to handle them seamlessly. Right, Yes, exactly like
- 15:15Speaker 1: you don't have to write complex file writing logic. When
- 15:17Speaker 1: your perse method yields that dictionary back to the framework,
- 15:21Speaker 1: scrapy looks at the dictionary keys url, model, mileage, price
- 15:25Speaker 1: and automatically turns them into the header columns of a spreadsheet.
- 15:28Speaker 1: It's beautiful, and the values become the rows. You just
- 15:31Speaker 1: run a command in your terminal like scrapy crawl true
- 15:34Speaker 1: godataset dot csv, and it constructs a perfectly formatted, clean
- 15:38Speaker 1: CSV file in real time.
- 15:40Speaker 2: It is practically coding magic when it works perfectly. But
- 15:44Speaker 2: as anyone who has done this knows, the data you
- 15:46Speaker 2: extract from HTML is rarely clean.
- 15:49Speaker 1: Oh let's talk about that, because there was a hilarious
- 15:51Speaker 1: and honestly frustrating real world problem with this specific car website.
- 15:56Speaker 1: Oh yeah, extracting the car's model year was easy, year
- 16:00Speaker 1: like twenty nineteen was just sitting perfectly inside a CSS class.
- 16:03Speaker 1: But extracting the actual model name like Tesla model three, Oh,
- 16:07Speaker 1: it was an absolute nightmare. What happened?
- 16:09Speaker 2: Well, when we pulled the string of text that was
- 16:11Speaker 2: supposed to say Tesla model three, we realized the website's
- 16:14Speaker 2: developers had randomly inserted raw HTML comments directly into the
- 16:19Speaker 2: middle of the text string.
- 16:20Speaker 1: So visualize this. Instead of a clean string that says
- 16:22Speaker 1: Tesla model three, the raw code looks something like Tesla
- 16:25Speaker 1: comment block tracking code Model comment block formatting text three.
- 16:29Speaker 1: The visible text was violently interrupted by invisible code comments
- 16:33Speaker 1: that the browser hides from human eyes, but the scraper
- 16:36Speaker 1: picks up. Why on Earth would a developer structure their
- 16:40Speaker 1: site like that?
- 16:42Speaker 2: Well, there are a few theories. Sometimes massive content management
- 16:46Speaker 2: systems or JavaScript frameworks generate bizarre, redundant code artifacts when
- 16:51Speaker 2: they compile a page. Okay, so just an accident, right,
- 16:55Speaker 2: But other times it's actually an intentional oppy station technique.
- 16:58Speaker 2: Really yeah, they know web scrapers look for clean text,
- 17:02Speaker 2: so they deliberately inject garbage into the HTML to make
- 17:05Speaker 2: the data messy and frustrate developers who are trying to
- 17:08Speaker 2: scrap their inventory.
- 17:09Speaker 1: It is exactly like panning for gold in a river,
- 17:12Speaker 1: but someone upstream is intentionally throwing garbage into your pan.
- 17:15Speaker 2: That's exactly what it is.
- 17:16Speaker 1: You don't just need define the gold. You have to
- 17:18Speaker 1: wash it off before you can actually keep it. So
- 17:20Speaker 1: what is the technical cleanup process there? How do you
- 17:23Speaker 1: wash the data before it hits the CSV file.
- 17:26Speaker 2: We had to implement a data cleaning step using Python's
- 17:29Speaker 2: built in string methods, specifically the dot replace method. So
- 17:34Speaker 2: before we added the car model to our dictionary, we
- 17:36Speaker 2: took that messy string and chained several dot replaced commands together.
- 17:41Speaker 2: We told Python to find those specific HTML comment tags
- 17:45Speaker 2: and replace them with absolutely nothing, an empty string, so
- 17:48Speaker 2: just delete some Essentially, Yeah, it scrubbed those characters out
- 17:52Speaker 2: of existence, and once the string was cleaned up and
- 17:55Speaker 2: looked like Tesla Model three again, then we finally saved
- 17:58Speaker 2: it to the dictionary.
- 18:00Speaker 1: Beverage is dealing with pure data. You're always dealing with
- 18:03Speaker 1: the eccentricities, the mistakes, or you know, the intentional roadblocks
- 18:06Speaker 1: of whoever built that specific website always, which brings us
- 18:10Speaker 1: to a critical realization about web scraping as a practice.
- 18:13Speaker 1: It seems incredibly fragile.
- 18:15Speaker 2: The technical term the industry uses is brittle brittle. Yeah,
- 18:19Speaker 2: you have to deeply understand that your elegant, streamlined twenty
- 18:23Speaker 2: line spider is relying entirely on the live structural architecture
- 18:27Speaker 2: of a website that you do not control.
- 18:29Speaker 1: So if a web designer at the car company decides
- 18:31Speaker 1: to update their user interface tomorrow, and they change the
- 18:34Speaker 1: name of a single CSS class SA, they change data
- 18:37Speaker 1: qua equals listings to data qua equals vehicle listings.
- 18:41Speaker 2: Your entire script breaks instantly, Just like that, Just like that,
- 18:46Speaker 2: the XPath coordinates you provided are suddenly pointing to an
- 18:49Speaker 2: empty field. Your scraper will run, it will connect, and
- 18:52Speaker 2: it will pull exactly zero data. Wow, what you build
- 18:56Speaker 2: to scrape a site today is completely dependent on the
- 18:59Speaker 2: site not changing tomorrow.
- 19:00Speaker 1: That sounds incredibly stressful for data engineers. I mean, if
- 19:04Speaker 1: the code is that brittle, how do you actually survive
- 19:08Speaker 1: and maintain these systems without losing your mind? Every time
- 19:11Speaker 1: a site updates.
- 19:12Speaker 2: You have to adopt a highly defensive mindset and follow
- 19:15Speaker 2: a few non negotiable best practices.
- 19:17Speaker 1: Okay, like what first?
- 19:18Speaker 2: When you are initially inspecting a web page's code using
- 19:21Speaker 2: your browser's developer tools to find your xpac coordinates, always
- 19:25Speaker 2: always use a private or incognito window.
- 19:28Speaker 1: Wait, why does an incognito window matter for looking at code?
- 19:31Speaker 2: Because your normal browser window carries a huge amount of state.
- 19:35Speaker 2: It has saved cookies, login sessions, cash files. Okay, many
- 19:39Speaker 2: modern websites will serve completely different HTML structures based on
- 19:42Speaker 2: whether you were logged in or even based on your
- 19:44Speaker 2: past browsing history, and.
- 19:46Speaker 1: Our bot doesn't have any of that.
- 19:47Speaker 2: Exactly, Your screeky bot arrives with a completely clean slate.
- 19:51Speaker 2: It has no cookies and no history. So by using
- 19:55Speaker 2: incognito mode, you ensure that the HTML structure you are
- 19:58Speaker 2: analyzing matches exactly what your cookie list spot is going
- 20:02Speaker 2: to encounter.
- 20:03Speaker 1: That makes perfect sense match the bot's environment. Okay, what
- 20:07Speaker 1: is another practice for surviving brittle code?
- 20:10Speaker 2: Local caching? When you are writing and testing your parse method,
- 20:14Speaker 2: do not constantly hit the live.
- 20:16Speaker 1: Server right because they might block you that and.
- 20:18Speaker 2: You need consistency, So save a local copy of the
- 20:21Speaker 2: downloaded HTML page to your hard drive. If a live
- 20:25Speaker 2: site updates its layout and your scraper suddenly breaks in production,
- 20:28Speaker 2: you have a frozen snapshot in time of the old code.
- 20:31Speaker 1: Oh that's smart.
- 20:32Speaker 2: Yeah, you can compare the local file to the live site,
- 20:34Speaker 2: see exactly what structural tags changed, and adjust your X
- 20:38Speaker 2: path coordinates without just guessing in the dark.
- 20:40Speaker 1: And I imagine using a property bugger is essential here too.
- 20:43Speaker 2: Absolutely, don't just rely on print statements to figure out
- 20:47Speaker 2: why a selector is failing, use a Python de bugger
- 20:50Speaker 2: in your code editor, set break points, and pause the
- 20:52Speaker 2: execution of your spider right in the middle of the
- 20:55Speaker 2: parse method. Step through your logic chunk by chunk, verifying
- 20:59Speaker 2: that ever every single XPath query is returning exactly what
- 21:03Speaker 2: you expect before you unleash the script on thousands of pages.
- 21:07Speaker 1: So before we wrap up this deep dive, let's do
- 21:10Speaker 1: a quick mental review exercise to lock this in.
- 21:12Speaker 2: Sounds good, let's do it.
- 21:13Speaker 1: Okay to the listener out there, Imagine you're building a
- 21:16Speaker 1: spider tonight to track programming textbook prices on a massive
- 21:20Speaker 1: online bookstore. Based on what we just learned, What are
- 21:24Speaker 1: the three steps you need to map out before you
- 21:26Speaker 1: even type a single line of your parser code.
- 21:28Speaker 2: Yeah, this is the perfect roadmap. So step one you
- 21:30Speaker 2: have to isolate the exact target URL right. Step two
- 21:33Speaker 2: you need to construct a custom user agent string so
- 21:36Speaker 2: the bookstores server doesn't immediately hand you a four h
- 21:39Speaker 2: three forbidden error for looking.
- 21:41Speaker 1: Like a bot. Gotta have the fake ID exactly.
- 21:43Speaker 2: And step three you need to open that URL in
- 21:46Speaker 2: an incognito window, inspect the raw HTML and find the
- 21:49Speaker 2: precise XPath coordinates that contain the book titles and prices.
- 21:53Speaker 2: Only then do you actually write the code.
- 21:55Speaker 1: You define the target, you disguise the request, and you
- 21:57Speaker 1: map the coordinates.
- 21:58Speaker 2: That's it.
- 21:59Speaker 1: But you know, there is one massive lingering challenge we
- 22:03Speaker 1: haven't touched on. Everything we've discussed today relies on one
- 22:07Speaker 1: foundational assumption that when scrapy requests the web page, the
- 22:12Speaker 1: server responds with standard HTML code containing the data.
- 22:16Speaker 2: Yes, and on the modern Internet, that assumption is becoming
- 22:19Speaker 2: less and less reliable every single day, right.
- 22:22Speaker 1: Because what happens if you point scrapey at a modern
- 22:24Speaker 1: dynamic website, maybe built with a framework like React or Angular,
- 22:28Speaker 1: where upon the initial load the HTML page is completely empty. Yeah,
- 22:32Speaker 1: like there are no car prices, no book titles. The
- 22:36Speaker 1: data is only generated a few seconds later by JavaScript
- 22:39Speaker 1: executing inside the user's browser.
- 22:41Speaker 2: This is really the frontier of webscraping right now. Standard
- 22:43Speaker 2: scrapy is essentially a lightning fast HTML parser, but it
- 22:47Speaker 2: is not a browser. Okay, it does not have a
- 22:50Speaker 2: JavaScript engine. It cannot execute code, so it cannot see
- 22:53Speaker 2: anything that the JavaScript is supposed to render on to
- 22:55Speaker 2: the page. To the spider, the page just looks totally blank.
- 22:58Speaker 1: So how do you extract data that technically isn't even
- 23:01Speaker 1: there yet. Well, we're going to leave that provocative question
- 23:05Speaker 1: for you to explore on your own. That's a fun one,
- 23:07Speaker 1: it really is. If you want to go deeper, look
- 23:10Speaker 1: at a headless browser automation tools like Selenium or Playwright.
- 23:13Speaker 1: They can actually spin up a hidden instance of Chrome,
- 23:16Speaker 1: let the JavaScript run, and then hand the fully rendered
- 23:19Speaker 1: code back to scrapy.
- 23:20Speaker 2: Yeah, it adds a massive layer of complexity, but it
- 23:23Speaker 2: is incredibly powerful, an.
- 23:25Speaker 1: Entirely new puzzle to solve. But with the foundation of
- 23:28Speaker 1: Scrapeye's asynchronous framework and a solid understanding of how HTTP
- 23:33Speaker 1: requests work, you have the architecture needed to tackle it definitely.
- 23:37Speaker 1: And when you finally get that automated machine humming perfectly,
- 23:40Speaker 1: quietly streaming thousands of data points into a clean CSV
- 23:44Speaker 1: file while you sit back and watch, you will never
- 23:46Speaker 1: want to manually copy and paste a spreadsheet ever again.
- 23:50Speaker 1: Happy data hunting.