Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 15: Mastering Items, Loaders, and Processing Pipelines
Transcript
- 0:00Speaker 1: Imagine it is, I don't know, like four point five
- 0:02Speaker 1: to five pm on a Friday.
- 0:04Speaker 2: The worst possible time, right.
- 0:06Speaker 1: At the absolute worst, and your boss drops by your
- 0:10Speaker 1: desk and hands you what sounds like an incredibly simple assignment.
- 0:14Speaker 1: They're like, hey, I need you to go to the
- 0:16Speaker 1: Yahoo Finance technology page and just pull the data for
- 0:20Speaker 1: every single tech stock listed in their main table.
- 0:23Speaker 3: It is a quick favor before the weekend, exactly.
- 0:26Speaker 1: And for every single company you need to grab four
- 0:30Speaker 1: specific things. You need the company name, the intra day
- 0:32Speaker 1: stock price, the stock symbol, and then the actual URL
- 0:36Speaker 1: that links to the company's full profile.
- 0:38Speaker 2: I mean, on the surface, that sounds like a five
- 0:40Speaker 2: minute task. You know, you open a spreadsheet, you open
- 0:43Speaker 2: your web browser, and you just get to.
- 0:44Speaker 1: Work, right, But then reality actually sets in because you're
- 0:48Speaker 1: highlighting the first company's name, you're hitting copy, tabbing over
- 0:50Speaker 1: to your spreadsheet, hitting paste. Yeah, then you're tabbing back
- 0:54Speaker 1: highlighting the price, copy tab paste, And you know, doing
- 0:56Speaker 1: that for one company is completely fine, but doing it
- 0:59Speaker 1: for a company, well, it's brutal. You are making hundreds
- 1:03Speaker 1: of highly repetitive, like precision clicks, Your risk starts aching,
- 1:07Speaker 1: your eyes are completely glazing over, and inevitably you start
- 1:10Speaker 1: making mistakes. You paste a price into the symbol column
- 1:14Speaker 1: by accident.
- 1:14Speaker 2: Which throws off the whole sheet exactly.
- 1:16Speaker 1: It is the kind of mind numbing, soul crushing manual
- 1:20Speaker 1: data entry that really makes you question your career choices.
- 1:23Speaker 2: It really does. But you know, humans are just fundamentally
- 1:28Speaker 2: unsuited for repetitive, high precision tasks over long durations. We
- 1:33Speaker 2: just lose focus. Computers however, I mean they thrive on
- 1:36Speaker 2: that exact type of work.
- 1:37Speaker 1: You don't get bored, No, not at all.
- 1:39Speaker 2: And this is the precise friction point where programming becomes
- 1:43Speaker 2: frankly practically.
- 1:44Speaker 1: Magic, which perfectly brings us to our mission for this
- 1:47Speaker 1: deep dive today. We're going to build a mental blueprint
- 1:50Speaker 1: of an automated data extraction pipeline, and we're looking really
- 1:54Speaker 1: closely at a Python framework called screepy.
- 1:56Speaker 3: Oh yeah, Scrapy is amazing for this, right.
- 1:58Speaker 1: It basically serves as the ultimate engine for this kind
- 2:01Speaker 1: of web scraping.
- 2:02Speaker 3: It really is a powerhouse.
- 2:03Speaker 2: And you know, our goal here for everyone listening isn't
- 2:06Speaker 2: just to talk about writing a quick duty script that
- 2:08Speaker 2: haphazardly yanks text off a page and calls.
- 2:11Speaker 3: It a day.
- 2:12Speaker 1: Right, we want to go deeper.
- 2:13Speaker 3: Exactly, we are.
- 2:14Speaker 2: Going to architect a robust, professional grade data pipeline. So
- 2:20Speaker 2: we will start by examining how to test raw code
- 2:23Speaker 2: in an isolated sandbox, figure out how to scale that
- 2:27Speaker 2: up to crawl entire pages, and eventually build a system
- 2:30Speaker 2: that automatically cleans and formats the data before saving it
- 2:35Speaker 2: into a pristine spreadsheet.
- 2:36Speaker 1: Okay, let's unpack this. Because whether you are a developer
- 2:40Speaker 1: prepping for a major data project, or you know, maybe
- 2:44Speaker 1: a self taught code or trying to automate your own life,
- 2:46Speaker 1: or just someone curious about how data actually moves around
- 2:49Speaker 1: the Internet, understanding this architecture gives you a whole new superpower.
- 2:53Speaker 1: So where do we even begin? Because if I'm staring
- 2:55Speaker 1: at that Yahoo Finance page, my first instinct is to just,
- 2:59Speaker 1: I don't know, write a man subscript and hope it
- 3:01Speaker 1: grabs the right number.
- 3:01Speaker 2: Yeah, and plunging straight into writing a massive script is
- 3:04Speaker 2: the fastest way to get completely.
- 3:05Speaker 1: Overwhelmed, I can imagine.
- 3:07Speaker 2: Because before building a factory, you need to test if
- 3:10Speaker 2: the raw materials are even usable. You need a testing
- 3:13Speaker 2: ground to see if the data you want is accessible
- 3:15Speaker 2: in the way you think it is, and in the
- 3:18Speaker 2: scrape ecosystem, this isolated environment is called the scrape shell.
- 3:22Speaker 1: The screepy shell. Okay, so I'm picturing like a digital sandbox.
- 3:26Speaker 3: That's a great way to look at it.
- 3:28Speaker 1: So you boot up this shell in your computer's terminal,
- 3:30Speaker 1: and you feed it the specific web address, like our
- 3:33Speaker 1: Yaku finance page, and it basically goes out, grabs the
- 3:37Speaker 1: underlying HTML of that page and downloads it right into
- 3:41Speaker 1: your sandbox exactly.
- 3:42Speaker 3: It pulls the raw code.
- 3:43Speaker 1: So now you can experiment with extracting the data without
- 3:46Speaker 1: the pressure of running a full automated program that might
- 3:50Speaker 1: break at any second.
- 3:51Speaker 2: Right, the sandbox isolates the problem.
- 3:53Speaker 3: You can just tinker.
- 3:54Speaker 2: But once you have the page data in there, you
- 3:56Speaker 2: need a designated place to actually put the specific fragments
- 3:59Speaker 2: of information you plan to extract. Okay, So the source
- 4:04Speaker 2: material introduces a really foundational concept here called a.
- 4:07Speaker 1: Scrapy item, Okay, an item. The way the documentation describes
- 4:11Speaker 1: this item, it actually reminds me of a bento box.
- 4:14Speaker 3: Oh I like that.
- 4:15Speaker 1: Yeah, because you have this container, but you don't just
- 4:18Speaker 1: dump all your food into one big, messy pile. You
- 4:21Speaker 1: have very specific pre labeled compartments. A compartment for the rice,
- 4:26Speaker 1: a compartment for the fish.
- 4:27Speaker 3: Right, it's structured.
- 4:28Speaker 1: Exactly in Python terms. It's like a highly structured dictionary.
- 4:32Speaker 1: We define our compartments or feels basically up front. So
- 4:36Speaker 1: we create a label for company name, one for inter
- 4:39Speaker 1: day price, one for symbol, and one for link.
- 4:42Speaker 2: The Bento box analogy perfectly captures the strict structural nature
- 4:46Speaker 2: of the item based class. When you first instantiate this
- 4:49Speaker 2: item in scrapy, it exists in memory as a completely
- 4:52Speaker 2: empty shell. The compartments are there, but they contain absolutely nothing.
- 4:56Speaker 1: You're just waiting for data, right.
- 4:58Speaker 2: Filling those compartments requires it to look at the underlined
- 5:01Speaker 2: structure of the website itself and extract the actual data.
- 5:04Speaker 1: Okay, and this is where it gets a little abstract,
- 5:06Speaker 1: I think, but it really is the core mechanism of webscreeping.
- 5:10Speaker 1: How do we actually tell our computer, hey, go look
- 5:13Speaker 1: at this massive wall of code and grab the words
- 5:16Speaker 1: Microsoft Corporation.
- 5:18Speaker 2: Yeah, it's not like you can just point at the screen.
- 5:20Speaker 2: So we use a mechanism called an XPath selector.
- 5:22Speaker 3: An XPath selector right.
- 5:24Speaker 2: To understand why XPath is necessary, we have to look
- 5:27Speaker 2: at how websites are actually built. HTML is not just
- 5:30Speaker 2: a flat text document. It is a complex, nested hierarchy
- 5:34Speaker 2: like a tree exactly.
- 5:35Speaker 3: It's a tree of elements.
- 5:37Speaker 2: You have this massive HTML trunk which branches off into
- 5:40Speaker 2: body tags, which branch off into dip containers, down into tables, rows,
- 5:44Speaker 2: and finally individual data cells. So to a computer, a
- 5:48Speaker 2: webpage looks like a massive set of Russian mesting dolls.
- 5:51Speaker 1: Okay, so an XPath is essentially like a set of
- 5:54Speaker 1: navigation directions through that specific family tree. Yes, you are
- 5:57Speaker 1: telling the scraper, okay, go into the table container of
- 6:00Speaker 1: the rows and look inside the cell at index two, right, and.
- 6:03Speaker 2: You've hit on a really vital detail there with index two.
- 6:06Speaker 2: Oh really, yeah, because in programming we generally start counting from.
- 6:10Speaker 1: Zero ah right, zero based indexing exactly.
- 6:13Speaker 2: So if the company name is in the third column
- 6:15Speaker 2: of the visual tail you see on the screen, it's
- 6:18Speaker 2: index in the actual code is two.
- 6:20Speaker 1: Wow.
- 6:21Speaker 3: Okay, yeah, if.
- 6:22Speaker 2: You tell your scraper to look at index three, you
- 6:24Speaker 2: are going to pull the entirely wrong column of data,
- 6:27Speaker 2: which is a very common trap when people first start
- 6:30Speaker 2: testing in the scrapey shell.
- 6:31Speaker 1: That would be so frustrating. Okay, so I write my
- 6:34Speaker 1: expath directions, I target index two. Does the shell just
- 6:38Speaker 1: like hand me the text Microsoft Corporation right then and there?
- 6:42Speaker 3: Uh No, it does not.
- 6:43Speaker 1: Oh it does. No.
- 6:44Speaker 2: The XPath selector merely returns a match. It basically hands
- 6:48Speaker 2: you a selector object, essentially a pointer that says, hey,
- 6:51Speaker 2: I found the digital.
- 6:52Speaker 1: Nesting doll you asked for, but it hasn't opened it yet.
- 6:54Speaker 2: Exactly the text you actually want is still sealed inside
- 6:57Speaker 2: that doll. So to reach in and extra the human
- 7:00Speaker 2: readable data, you have to execute a specific function on
- 7:04Speaker 2: that select or object.
- 7:05Speaker 1: Okay, looking at the material, it seems we use functions
- 7:07Speaker 1: like get for this.
- 7:08Speaker 3: Yes, get is the one.
- 7:10Speaker 1: So the workflow is basically, use x path to point
- 7:13Speaker 1: at the cell, then used get to physically pull the
- 7:16Speaker 1: string of text out, and once we have that text,
- 7:18Speaker 1: we place it neatly into the company name compartment of
- 7:21Speaker 1: our Bento box.
- 7:22Speaker 3: You got it.
- 7:23Speaker 2: That completes the entire cycle for a single data point.
- 7:27Speaker 2: We've successfully navigated the HTML tree, extracted the raw text,
- 7:31Speaker 2: and structured it. But we did this inside an isolated
- 7:34Speaker 2: sandbox for one single company.
- 7:37Speaker 1: Right, And here's the fatal flaw with staying in the sandbox.
- 7:41Speaker 1: We are still manually pointing it at one company at
- 7:43Speaker 1: a time. If we stop here, we haven't actually automated anything,
- 7:47Speaker 1: have we. We have just changed the interface from copy
- 7:49Speaker 1: pasting in a web browser to typing manual commands in
- 7:52Speaker 1: a terminal.
- 7:53Speaker 3: Exactly. It's the same amount of work.
- 7:54Speaker 1: So to build a factory floor that can do this
- 7:56Speaker 1: one hundred times a second, we need a way to
- 7:58Speaker 1: scale the logic.
- 7:59Speaker 2: Yeah, moving from testing to actual automation requires building a spider.
- 8:03Speaker 1: A spider right.
- 8:04Speaker 2: When you are ready to scale, you create a full
- 8:06Speaker 2: scrapy project. Running a simple setup command generates an entire
- 8:11Speaker 2: architecture of folders and files. It essentially lays the concrete
- 8:15Speaker 2: for your factory floor.
- 8:16Speaker 1: Okay, so inside that new folder structure, we have a
- 8:19Speaker 1: file called items dot pie. I assume this is where
- 8:23Speaker 1: we officially encode the blueprint for our Bento box. Right,
- 8:26Speaker 1: listing out those four specific fields we want to fill.
- 8:29Speaker 2: Yes, you define your item there, but the actual end
- 8:31Speaker 2: of the thing doing the crawling lives in a separate file,
- 8:33Speaker 2: maybe something we call a companydetailseem dot.
- 8:36Speaker 3: Pie got it.
- 8:37Speaker 1: And that is the spider file exactly.
- 8:39Speaker 2: And the logic you encode inside the spider is incredibly
- 8:43Speaker 2: elegant because it relies on loops ah loops.
- 8:46Speaker 3: Yeah.
- 8:47Speaker 2: Instead of writing separate XPath directions for every single row
- 8:50Speaker 2: on the Yahoo finance page, you write a generalized set
- 8:53Speaker 2: of instructions. You instruct the spider to target the entire
- 8:56Speaker 2: table first, then you tell it to iterate. So row
- 9:00Speaker 2: by row right, you say, look at row one, perform
- 9:03Speaker 2: these actions, then move to row two and repeat.
- 9:05Speaker 1: Okay, so for row one, it instantiates a brand new
- 9:08Speaker 1: empty Bento box. It runs the x path directions to
- 9:11Speaker 1: grab the name, price, symbol, and link. It packs them
- 9:14Speaker 1: all into the box. But then it uses a command
- 9:16Speaker 1: called yield. Yes, yield, And here is where I need
- 9:19Speaker 1: to clarify something, because yield sounds a bit mysterious. If
- 9:22Speaker 1: I tell the program to yield my carefully packed box,
- 9:27Speaker 1: am I just throwing it out the window into the void?
- 9:30Speaker 1: Like how are we actually capturing this data.
- 9:33Speaker 2: It's a very common point of confusion, actually, because in
- 9:36Speaker 2: standard Python functions, people are really used to the return command. Right.
- 9:40Speaker 1: Return just gives you the answer and stops.
- 9:42Speaker 2: Exactly, it hands back a value and immediately kills the function.
- 9:46Speaker 2: But yield is fundamentally different. It creates what's called a generator.
- 9:50Speaker 1: A generator.
- 9:51Speaker 2: Okay, so when the spider yields the item, it hands
- 9:54Speaker 2: the Bento box up to the Scrapy engine, basically pauses
- 9:58Speaker 2: its current state, and then immediately turns back to the
- 10:01Speaker 2: loop to process road too.
- 10:02Speaker 1: Oh I see, so it operates almost like a worker
- 10:04Speaker 1: on an assembly line.
- 10:05Speaker 3: Yeah, perfect analogy.
- 10:07Speaker 1: They finish packing a box, they hand it to the
- 10:09Speaker 1: supervisor standing directly behind them, and then instantly turn back
- 10:12Speaker 1: to the conveyor belt to pack the next one. They
- 10:14Speaker 1: don't clock out and go home just because one single
- 10:16Speaker 1: box is done.
- 10:17Speaker 3: Exactly.
- 10:18Speaker 2: The supervisor in your analogy is Scrapy itself. If you
- 10:22Speaker 2: go to your terminal and simply tope screaby crawl company
- 10:25Speaker 2: details item, Scrapey spins up the spider, The spider starts
- 10:29Speaker 2: iterating yielding those boxes, and Scrapey just prints the resulting
- 10:32Speaker 2: Jason objects right there on your screen, cascading down the terminal.
- 10:36Speaker 1: I mean, seeing the matrix code fall down the screen
- 10:38Speaker 1: is really fun. But data printed in a terminal basically
- 10:41Speaker 1: disappears the second you close the window. We need a
- 10:44Speaker 1: physical spadsheet we can actually send to our boss.
- 10:46Speaker 2: Oh absolutely, And scrapey makes saving the data almost shockingly simple.
- 10:51Speaker 2: Really yeah, you do not need to write complex file
- 10:53Speaker 2: handling code inside your spider at all. You simply modify
- 10:57Speaker 2: the terminal command you just ran. Oh nice, By adding
- 10:59Speaker 2: an out put flag, specifically appending augiocompanydetails dot csv to
- 11:04Speaker 2: the end of your command, scrape basically catches every single
- 11:07Speaker 2: item the spider yields and dynamically appends into a neatly
- 11:10Speaker 2: formatted spreadsheet file.
- 11:12Speaker 1: Wait, really, so one simple terminal flag replaces dozens of
- 11:16Speaker 1: lines of file saving logic just one flag.
- 11:18Speaker 3: That's it.
- 11:19Speaker 1: That is amazing. We run the command, the spider crawls
- 11:21Speaker 1: the page, and boom, we have a CSV file sitting
- 11:24Speaker 1: on our hard drive. But you know, if we open
- 11:26Speaker 1: that spreadsheet right now, I have a feeling it is
- 11:28Speaker 1: going to be a disaster. The web is inherently messy,
- 11:32Speaker 1: right and raw scraped data is rarely usable right out
- 11:36Speaker 1: of the box.
- 11:37Speaker 2: Oh, it's almost always unusable. Raw data is rough. The
- 11:41Speaker 2: source material points out two glaring formatting issues. You will
- 11:44Speaker 2: immediately encounter when scraping that Yahoo Finance page.
- 11:48Speaker 1: Okay, what's the first one.
- 11:49Speaker 2: The first problem has to do with the URLs we
- 11:51Speaker 2: are extracting for the company profiles. They are what developers
- 11:54Speaker 2: call relative.
- 11:55Speaker 1: Links, a relative links, meaning they don't start with HTP
- 11:59Speaker 1: or wa ww. The HTML might just say something like
- 12:03Speaker 1: quote MSFT exactly because websites do this to save space, right, yeah,
- 12:07Speaker 1: they assume the browser already knows you are sitting on
- 12:09Speaker 1: the Yahoo Finance domain. But if you drop that relative
- 12:13Speaker 1: link into an offline Excel file and click it, your
- 12:15Speaker 1: computer has literally no idea where to go. It's a
- 12:18Speaker 1: completely broken link.
- 12:19Speaker 2: Right, The spreadsheet has completely lost the context of the website.
- 12:23Speaker 2: So that is the first major hurdle, appending the base
- 12:26Speaker 2: domain to every single link on.
- 12:28Speaker 1: The second issue.
- 12:29Speaker 2: The second issue is a weird quirk of how Scrape's
- 12:32Speaker 2: extraction functions work. By default, when you tell scrape to
- 12:36Speaker 2: extract text, it doesn't just hand you a clean string.
- 12:39Speaker 2: It returns a Python list object.
- 12:41Speaker 1: Oh I see. It assumes there might be a multiple
- 12:44Speaker 1: matches for your expath, so it wraps everything in a
- 12:46Speaker 1: list just to be.
- 12:47Speaker 3: Safe, exactly, just in case.
- 12:49Speaker 1: Which means our spreadsheet doesn't say Microsoft Corporation. It says
- 12:52Speaker 1: bracket quote Microsoft Corporation quote bracket like it's literally trapped
- 12:57Speaker 1: inside Microsoft Corporation.
- 12:59Speaker 2: Yeah, and if you try to hand a spreadsheet full
- 13:01Speaker 2: of bracketed list objects to a data analyst, they will
- 13:04Speaker 2: send it right.
- 13:04Speaker 3: Back to you.
- 13:05Speaker 1: I would too.
- 13:06Speaker 2: You cannot perform basic database functions on text that's trapped
- 13:09Speaker 2: in a list format. We need a way to refine
- 13:11Speaker 2: this data systematically, and this introduces a vital scrapy component
- 13:15Speaker 2: called the item loader.
- 13:16Speaker 1: The item loader. Okay, so instead of the spider just
- 13:19Speaker 1: stuffing raw, dirty data directly into our bento box fields,
- 13:23Speaker 1: an item loader acts as an intelligent populator. Yes, we
- 13:26Speaker 1: feed the raw XPath data into the loader and it processes, it,
- 13:30Speaker 1: cleans it, and formats it before permanently sealing the box.
- 13:33Speaker 1: And based on the documentation, it achieves this using two
- 13:37Speaker 1: distinct mechanisms, input processors and output processor.
- 13:41Speaker 3: You've got it.
- 13:41Speaker 2: Let's examine the input processor first. Okay, This mechanism intercepts
- 13:45Speaker 2: the data the exact millisecond it is scraped off the
- 13:48Speaker 2: page before it even touches the item. Think about our
- 13:51Speaker 2: prokin relative URL.
- 13:52Speaker 1: Problem right, the quote MSFT problem.
- 13:55Speaker 2: Right In our blueprint file items dot piy, we can
- 13:58Speaker 2: attach an input process a specifically to the link field.
- 14:02Speaker 2: And that documentation highlights a really powerful object for this
- 14:05Speaker 2: called map compose map composed.
- 14:07Speaker 1: I mean it sounds a bit complicated, but the way
- 14:08Speaker 1: I understand it, it is essentially a midair interceptor.
- 14:11Speaker 3: I like that.
- 14:12Speaker 1: Yes, So you write a tiny helper function let's just
- 14:15Speaker 1: name it pullings ah. And the only job of pullinks
- 14:18Speaker 1: is to take a broken piece of text like quote
- 14:20Speaker 1: MSFT and just gluefinance dot yahoo dot com to the
- 14:24Speaker 1: front of it exactly.
- 14:25Speaker 2: And map compose allows you to seamlessly map that custom
- 14:28Speaker 2: pullings function to your incoming data stream. So the raw
- 14:32Speaker 2: relative link comes off the web page, map composed catches
- 14:34Speaker 2: it in midair, feeds it through your helper function, and
- 14:37Speaker 2: the fully repaired absolute URL is what actually gets placed
- 14:41Speaker 2: into the Bento box.
- 14:42Speaker 1: That is so clean, So that handles the data on
- 14:45Speaker 1: the way in. But what about the bracket problem that
- 14:48Speaker 1: messy list format the Microsoft Corporation issue?
- 14:51Speaker 2: Right?
- 14:51Speaker 1: I assume that is where output processors step in. If
- 14:53Speaker 1: the input processor is washing the ingredients before they go
- 14:56Speaker 1: into the box. The output processor operates at the very
- 14:58Speaker 1: end of the line, right before the spider yields the
- 15:01Speaker 1: final product.
- 15:02Speaker 2: Precisely, output processors handle the final data transformation, and Scrapie
- 15:06Speaker 2: actually provides a built in output processor explicitly designed to
- 15:11Speaker 2: solve this exact list problem.
- 15:13Speaker 3: Oh really, yeah, it'll call take first.
- 15:14Speaker 1: Take first. I was actually trying to think of an
- 15:16Speaker 1: analogy for this earlier. It's almost like shucking an oyster.
- 15:19Speaker 2: Shucking an oyster. Okay, how so, Well.
- 15:21Speaker 1: The data you want is the pearl, but it's trapped
- 15:24Speaker 1: inside this totally useless hardshell the list brackets. The take
- 15:29Speaker 1: first processor prize open, the list reaches in, takes the
- 15:33Speaker 1: very first item it finds, and just hands you the clean,
- 15:36Speaker 1: unencumbered pearl of text.
- 15:38Speaker 3: That is a perfect analogy. Yes.
- 15:40Speaker 2: By setting take first as the default output processor for
- 15:44Speaker 2: your item loader, every single field, the company, name, the price,
- 15:49Speaker 2: the symbol is automatically shucked from those annoying brackets. Nice.
- 15:52Speaker 2: They all become clean, plain strengths. So now when you
- 15:55Speaker 2: save to your CSV, the spreadsheet looks completely professional.
- 15:58Speaker 1: Okay, I am really loving this architecture. Item loaders give
- 16:01Speaker 1: us structural cleaning on the fly. But let's push this
- 16:04Speaker 1: even further.
- 16:05Speaker 3: Let's do it.
- 16:05Speaker 1: What if we want to apply actual business logic, Like,
- 16:08Speaker 1: what if we aren't just formatting text, but we want
- 16:11Speaker 1: to make active decisions based on the data itself after
- 16:14Speaker 1: the whole extraction process is complete.
- 16:16Speaker 2: Well, structural cleaning is one thing, but applying business logic
- 16:19Speaker 2: requires the final and perhaps most powerful piece of this
- 16:23Speaker 2: scrapy architecture. Data pipelines.
- 16:26Speaker 1: Data pipelines. I visualize a pipeline as a quality assurance
- 16:30Speaker 1: conveyor belt on a factory floor.
- 16:32Speaker 3: Oh absolutely so.
- 16:33Speaker 1: Our item loaders have packed these perfectly formatted bento boxes.
- 16:38Speaker 1: Now those completed boxes are rolling down a long conveyor belt,
- 16:42Speaker 1: and positioned along this belt are different QA stations. As
- 16:45Speaker 1: a box rolls up to a station, an inspector opens it,
- 16:49Speaker 1: looks at the data inside based on very strict mathematical rules,
- 16:53Speaker 1: and decides what to do. They can modify the data,
- 16:55Speaker 1: they can drop the box entirely into the trash, or
- 16:58Speaker 1: they can simply wave it through to the next station.
- 17:00Speaker 2: The conveyor belt analogy is incredibly accurate in your scraping project.
- 17:04Speaker 2: These pipelines live in their own dedicated file simply named
- 17:07Speaker 2: pipelines dot pi makes sense, and inside this file you
- 17:10Speaker 2: can construct as many of these QA stations as you need.
- 17:13Speaker 2: Each station is written as a standard Python class, and
- 17:16Speaker 2: each class must contain a specific mandatory function called process item.
- 17:20Speaker 1: Because its entire existence is dedicated to processing the item currently.
- 17:24Speaker 3: Rolling down the belt exactly.
- 17:26Speaker 1: The source material actually provides a highly practical example of
- 17:30Speaker 1: this business logic involving the stock prices. Let's walk through
- 17:33Speaker 1: how we build that.
- 17:35Speaker 3: Sure.
- 17:35Speaker 2: So imagine you are building a tool for a financial
- 17:38Speaker 2: analyst who only cares about extreme stock price movements. They
- 17:43Speaker 2: want to flag any stock over one hundred dollars or
- 17:45Speaker 2: under fifty dollars. Okay, we can build two separate stations
- 17:48Speaker 2: on our conveyor belt. We define our first class and
- 17:51Speaker 2: name it above one hundred price check.
- 17:53Speaker 1: Station one is operational the item rolls up. What actually
- 17:56Speaker 1: happens inside that process item function.
- 17:59Speaker 2: First, it looks at the inter day price field. Now,
- 18:01Speaker 2: remember the price is currently stored as a string of text.
- 18:04Speaker 2: You cannot perform mathematical operations on text.
- 18:06Speaker 1: Right, Python would just throw an error exactly.
- 18:09Speaker 2: So the first thing the function does is parse that
- 18:10Speaker 2: text and convert it into a numerical float. Once it
- 18:13Speaker 2: is a real number, it as a simple logical question,
- 18:16Speaker 2: is this value greater than one hundred?
- 18:18Speaker 1: Okay, let's say the extracted price is a one oh
- 18:20Speaker 1: five point fifty. The answer is yes. What does the
- 18:24Speaker 1: station actually do?
- 18:25Speaker 2: It actively overwrites the scrape data. It modifies the item,
- 18:29Speaker 2: changing the price field from one oh five point fifty
- 18:31Speaker 2: to a hard coded string that simply reads one hundred.
- 18:34Speaker 1: Oh wow, so it replaces the number entirely.
- 18:37Speaker 3: Yeah.
- 18:37Speaker 2: It categorizes the data, and once the modification is done,
- 18:40Speaker 2: it passes the item down the belt.
- 18:41Speaker 1: Okay, So the item continues rolling and arrives at our
- 18:43Speaker 1: second class below fifty price check station two. I assume
- 18:47Speaker 1: the logic mirror is the first. It converts the price
- 18:50Speaker 1: to a number and checks if it is under fifty dollars. So,
- 18:52Speaker 1: if the stock is trading at say forty dollars, it
- 18:55Speaker 1: overwrites the field to read fifty precisely.
- 18:57Speaker 2: But the really fascinating part of this pipeline is what
- 19:00Speaker 2: happens to the data that followed perfectly in the middle.
- 19:03Speaker 2: Consider a stock trading at exactly seventy five dollars.
- 19:06Speaker 1: Okay, So the seventy five dollars box rolls up to
- 19:08Speaker 1: station one. The above one hundred check runs seventy five
- 19:12Speaker 1: is not greater than one hundred, so the inspector does nothing.
- 19:15Speaker 1: The box passes through completely untouched. It reaches station two.
- 19:18Speaker 1: The below fifty check runs seventy five is not less
- 19:21Speaker 1: than fifty. Again, the inspector does nothing, and.
- 19:23Speaker 2: So the seventy five dollars item arrives at the very
- 19:26Speaker 2: end of the conveyor belt completely untouched, retaining its original
- 19:29Speaker 2: precise numerical value.
- 19:31Speaker 1: That is so cool.
- 19:32Speaker 2: You see, you are not just blindly scraping data anymore.
- 19:36Speaker 2: You are dynamically categorizing and filtering information in real time.
- 19:41Speaker 1: And if you are listening to this and wondering why
- 19:44Speaker 1: go through all this trouble, why not just dump the
- 19:47Speaker 1: raw numbers into Excel and run a simple filter formula later.
- 19:51Speaker 3: Right? People always ask.
- 19:52Speaker 1: That it's because of automation. By building this logic directly
- 19:56Speaker 1: into the pipeline, this script can run unattended on a
- 19:59Speaker 1: server every single day for a year. You wake up
- 20:02Speaker 1: and your perfectly categorized, cleaned spreadsheet is just sitting there
- 20:07Speaker 1: waiting for you, no manual cleanup required.
- 20:09Speaker 2: Ever, it's huge it's just your time from cleaning data
- 20:12Speaker 2: to actually analyzing it exactly. But building a pipeline with
- 20:15Speaker 2: multiple stations introduces a really critical logistical problem.
- 20:19Speaker 1: Oh what's that?
- 20:21Speaker 2: Well, if you have a massive pipeline with twenty different
- 20:23Speaker 2: stations doing complex transformations, the order in which those stations.
- 20:27Speaker 3: Operate is paramount.
- 20:28Speaker 1: Oh I see.
- 20:29Speaker 3: Yeah.
- 20:29Speaker 2: If station one deletes a piece of data entirely, station
- 20:32Speaker 2: two cannot check it, it's gone.
- 20:34Speaker 1: Okay, So how does scrapee manage the traffic? Like, how
- 20:36Speaker 1: does it know that the above one hundred check should
- 20:38Speaker 1: run before the blow fifty check?
- 20:40Speaker 2: Scrapeye requires you to explicitly declare the order in a
- 20:44Speaker 2: configuration file called settings dot pie. Now, by default, Scrapeye
- 20:49Speaker 2: is a very lightweight crawler, so pipelines are turned off
- 20:51Speaker 2: to save memory.
- 20:52Speaker 1: Oh I didn't realize that.
- 20:53Speaker 2: Yeah, So to activate your conveyor belt, you must open
- 20:57Speaker 2: settings dot pie and actually uncomment a specific dictionary named itempipelines.
- 21:02Speaker 1: Ah. So settings dot pie is essentially the control room
- 21:05Speaker 1: of the factory. You uncomment the code to power up
- 21:08Speaker 1: the conveyor.
- 21:08Speaker 2: Belt exactly, and inside that dictionary you list have the
- 21:10Speaker 2: names of your classes, your QA stations, and next to
- 21:13Speaker 2: each class name, you assigned an integer a priority number. Okay,
- 21:16Speaker 2: and Scrape operates on a very strict rule. Here, a
- 21:19Speaker 2: lower number dictates a higher priority.
- 21:22Speaker 1: So it's exactly like a race. First place gets the
- 21:24Speaker 1: number one, so they go first.
- 21:25Speaker 3: Exactly.
- 21:26Speaker 1: So if I assign above one hundred price check a
- 21:28Speaker 1: priority number of one hundred and below fifty price check
- 21:31Speaker 1: a priority number of two hundred, Scrape basically guarantees that
- 21:34Speaker 1: the one hundred check executes first every single time.
- 21:37Speaker 2: Yeap, you hold absolute control over the sequence of transformations.
- 21:41Speaker 2: You can inject new stations, you can reorder them or
- 21:44Speaker 2: turn them off completely, all from the central settings dot
- 21:46Speaker 2: PI file without ever having to touch your underlying spider code.
- 21:50Speaker 1: Man, let's zoom out and look at what we have
- 21:52Speaker 1: constructed today. We didn't just write a script. We architected
- 21:56Speaker 1: a complete resilient system.
- 21:58Speaker 3: We really did.
- 21:59Speaker 1: We started in the scriptshall testing the HTML dawntree to
- 22:02Speaker 1: figure out our XPath navigation. We built a structural blueprint
- 22:06Speaker 1: for our data using the item bento box. We deployed
- 22:10Speaker 1: a spider to loop through rows and continually yield items.
- 22:13Speaker 1: We utilized item loaders acting as mid air interceptors and
- 22:17Speaker 1: oyster shuckers to clean up broken URLs and messy brackets,
- 22:21Speaker 1: and finally we routed everything through a QA conveyor belt
- 22:24Speaker 1: in the pipeline to apply custom business logic.
- 22:27Speaker 2: Moving from a manual task to a fully automated self
- 22:30Speaker 2: cleaning data pipeline completely alters how you viewed data on
- 22:34Speaker 2: the Internet.
- 22:35Speaker 1: It really does. So to make sure the core mechanisms
- 22:38Speaker 1: of this architecture really stick with you, let's see if
- 22:40Speaker 1: we actually internalize the details. A quick review question for
- 22:43Speaker 1: everyone listening. What's your if I run my spider and
- 22:45Speaker 1: I open my CSV file and I find that my
- 22:47Speaker 1: spreadsheet is full of data trapped inside those annoying list brackets,
- 22:52Speaker 1: what is the one word fix I forgot to implement
- 22:55Speaker 1: in my item loader?
- 22:56Speaker 2: You forgot your oyster shuck the take first output processor
- 22:59Speaker 2: take first.
- 23:00Speaker 1: It is such a tiny detail in the grand architecture,
- 23:02Speaker 1: but you know, knowing the default tools available in the
- 23:05Speaker 1: framework is exactly what separates a fragile, messy script from
- 23:09Speaker 1: a professional, production ready pipeline.
- 23:12Speaker 2: Absolutely, a professional pipeline is designed to be resilient, but
- 23:16Speaker 2: you know there is an external factor that even the
- 23:18Speaker 2: best pipeline cannot inherently control.
- 23:21Speaker 1: The web itself.
- 23:22Speaker 2: Exactly the web itself. Our entire beautifully calibrated system relies
- 23:26Speaker 2: on XPath selectors. We gave our spider rigid directions based
- 23:30Speaker 2: on the current lay out of the website. We told
- 23:32Speaker 2: it navigate into the table, drop down into the rows,
- 23:34Speaker 2: find the solid index too, right.
- 23:36Speaker 1: So what happens when Yahoo Finance decides to just refresh
- 23:39Speaker 1: their brand overnight, like they roll out massive UI redesign.
- 23:42Speaker 1: They ditch HTML tables entirely and move to a modern
- 23:46Speaker 1: grid layout.
- 23:46Speaker 2: The moment they push that update, the structure of the
- 23:49Speaker 2: HTML tree fundamentally changes. Your XPath directions become completely useless.
- 23:55Speaker 2: The spider navigates to where index two used to be
- 23:58Speaker 2: and finds nothing or worse. Or worse, it finds entirely
- 24:01Speaker 2: different data, like grabbing the market caap instead of the
- 24:03Speaker 2: company name, and blindly feeds that bad data straight through
- 24:07Speaker 2: your beautiful pipeline into your final spreadsheet.
- 24:09Speaker 1: The pipeline executed perfectly, but the map it used was
- 24:12Speaker 1: completely obsolete. It highlights a massive challenge in this field,
- 24:16Speaker 1: because now that you understand how to build a scripey
- 24:18Speaker 1: pipeline that collects cleans and transforms data. The next logical
- 24:22Speaker 1: frontier is resiliency. How do you design systems that don't
- 24:26Speaker 1: just extract data based on today's layout, but can intelligently
- 24:29Speaker 1: hunt for that data tomorrow, adapting seamlessly when the digital
- 24:32Speaker 1: landscape shifts completely underneath them. Something to think about.