Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 31: From Item Loaders to Pipelines
Transcript
- 0:00Speaker 1: You've shared some incredibly relatable notes with us lately about
- 0:05Speaker 1: the absolute headache that is raw webscraping, Like you know,
- 0:09Speaker 1: the exact feeling we were talking about here.
- 0:11Speaker 2: Oh yeah, completely. It's a universal pain point.
- 0:14Speaker 3: Right.
- 0:14Speaker 1: You write a script, You ask your code to go
- 0:16Speaker 1: out and fetch data maybe I don't know prices, book titles,
- 0:20Speaker 1: contact information, and instead of giving you a neat spreadsheet,
- 0:23Speaker 1: your script basically kicks open your front door and dumps
- 0:27Speaker 1: a massive, unorganized, completely unstructured pile of raw HTML tags
- 0:33Speaker 1: right in the middle of.
- 0:34Speaker 2: Your floor, which is exactly the painful reality of data
- 0:38Speaker 2: extraction when you first start out as a developer. I mean,
- 0:40Speaker 2: you successfully get the data, which is great, but it
- 0:42Speaker 2: is just buried under weird formatting, Unicode errors and broken
- 0:46Speaker 2: relative lengths.
- 0:46Speaker 3: It's a total mess, exactly.
- 0:48Speaker 2: It's practically unusable in that raw state.
- 0:50Speaker 1: Precisely, and that is the mission for our deep dive today.
- 0:54Speaker 1: We are going fundamentally beyond just fetching pages. We are
- 0:58Speaker 1: architecting a clean, highly efficient, basically enterprise grade data extraction machine.
- 1:03Speaker 2: I love the sound of that.
- 1:04Speaker 1: We are going to walk through this step by step
- 1:06Speaker 1: logic of taking that raw chaotic web data and transforming
- 1:11Speaker 1: it into polished, validated information using built in framework tools.
- 1:15Speaker 3: Because let's be real.
- 1:17Speaker 1: Nobody wants a pile of digital garbage crashing their database.
- 1:20Speaker 2: What's fascinating here is the necessary shift in a developer's
- 1:24Speaker 2: mindset because scraping isn't just about grabbing data. Frankly, anyone
- 1:29Speaker 2: can write a script to grab data, right.
- 1:31Speaker 1: The grabbing part is easy, exactly.
- 1:33Speaker 2: Professional level scraping is entirely about how you structure that
- 1:36Speaker 2: data efficiently while it is in transit. So today we
- 1:40Speaker 2: are really analyzing the journey that data takes. Okay, we
- 1:43Speaker 2: will explore the mechanisms to collect it cleanly at the
- 1:46Speaker 2: source and then crucially, how to route it through an
- 1:49Speaker 2: automated sequential assembly line for processing and validation.
- 1:52Speaker 1: Let's dive right into that source collection part, because before
- 1:55Speaker 1: we can process any information at all, we need a reliable,
- 1:58Speaker 1: scalable way to pick the actual items out of that
- 2:01Speaker 1: missy pile of markup and the materials you've shared with us.
- 2:04Speaker 1: Introduce a well a highly specific tool for this called
- 2:07Speaker 1: an item loader.
- 2:08Speaker 2: Yes, item loaders. In software engineering, we love encapsulation, right,
- 2:13Speaker 2: and item loaders are these delightfully convenient mechanisms designed specifically
- 2:18Speaker 2: to populate data objects.
- 2:20Speaker 1: Okay, can you give me an example of how that looks.
- 2:22Speaker 2: Sure, Let's imagine you are scraping a bookstore. You have
- 2:26Speaker 2: a digital blueprint defined in your code for what a
- 2:28Speaker 2: book should look like. That's your book item class, right,
- 2:31Speaker 2: the template exactly. The item loader is the specialized tool
- 2:34Speaker 2: you use to safely inject the actual extracted data into
- 2:37Speaker 2: that book object.
- 2:39Speaker 1: Tracking with you so far, But how does that logic
- 2:43Speaker 1: actually execute in the code like step by step?
- 2:46Speaker 2: Well, it begins with initialization at the top of your script.
- 2:48Speaker 2: You import the item loader class. Then, when your spider
- 2:52Speaker 2: hits a web page, you instantiate an item loader object okay,
- 2:55Speaker 2: and you pass it to critical arguments. First the specific
- 2:59Speaker 2: class you want to build, which is your book item. Second,
- 3:01Speaker 2: you hand it the raw response data that your spider.
- 3:04Speaker 1: Just downloaded, which is that messy pile of code on
- 3:07Speaker 1: our floor.
- 3:08Speaker 2: Precisely, that response.
- 3:09Speaker 1: Is the pile. Okay, let's unpack this. So the loader
- 3:12Speaker 1: is currently holding an empty box the book object, and
- 3:15Speaker 1: it is looking at the pile of raw web data.
- 3:19Speaker 1: But it needs a mechanism to actually move the data
- 3:21Speaker 1: into the box.
- 3:22Speaker 2: Right, Yes, exactly, And for every single field you want
- 3:26Speaker 2: to populate, say the title, the author, or the price,
- 3:30Speaker 2: you use specific extraction methods built right into the loader.
- 3:33Speaker 2: Like what the most common ones you will use are
- 3:35Speaker 2: ad XPath, add serial tip, or add value.
- 3:39Speaker 3: Oh okay.
- 3:39Speaker 2: Think of XPath and CSS selectors as like highly specific
- 3:43Speaker 2: GPS coordinates on a web page's document object model or down.
- 3:48Speaker 3: I like the GPS coordinates.
- 3:50Speaker 2: Yeah, and these loader methods act as targeted grabbers. They
- 3:53Speaker 2: reach into the raw HTML, pull out the exact string
- 3:56Speaker 2: value at those coordinates, and stage it for the book object.
- 3:59Speaker 1: I think I have a good announology for this that
- 4:00Speaker 1: builds on the problem of the messy pile. Is an
- 4:03Speaker 1: item loader essentially like sending a very literal, highly trained
- 4:07Speaker 1: personal shopper into a massive grocery store.
- 4:09Speaker 2: A game listening.
- 4:10Speaker 1: You have them a specific list that is your item
- 4:12Speaker 1: class the book object, and they know exactly which aisles
- 4:16Speaker 1: and shelves to visit. Those are the XPath or CSS
- 4:19Speaker 1: selectors to grab the exact raw goods you asked for,
- 4:23Speaker 1: completely ignoring all the other noise in the store.
- 4:25Speaker 2: That is a brilliant way to visualize it. Actually, the
- 4:28Speaker 2: personal shopper doesn't get distracted by the promotional displays or
- 4:32Speaker 2: the layout of the store. They just navigate directly to
- 4:35Speaker 2: the precise coordinates you provided, grab the target item, and
- 4:38Speaker 2: place it in their cart.
- 4:39Speaker 1: But you know, as a developer, I have to push
- 4:42Speaker 1: back a little here. Sure, why do we need to
- 4:44Speaker 1: instantiate this specific loader object to act as the personal shopper?
- 4:49Speaker 1: I mean, if I already know the xpass coordinates, why
- 4:52Speaker 1: can't I just write a basic Python dictionary, grab the
- 4:55Speaker 1: data manually in my code, and assign.
- 4:57Speaker 2: It to a variable like a standard key value pair.
- 4:59Speaker 1: Exactly, something like item title equals response dot x path
- 5:02Speaker 1: and so on. It just feels like we are adding
- 5:04Speaker 1: an extra layer of abstraction for absolutely no reason.
- 5:07Speaker 2: It is a totally fair question, and honestly, it is
- 5:10Speaker 2: a trap a lot of self taught developers fall into.
- 5:13Speaker 2: Oh really, yeah, The reason you use an item loader
- 5:16Speaker 2: is separation of concerns. When you grab data manually and
- 5:19Speaker 2: stuff it into a dictionary, your core spider code gets
- 5:23Speaker 2: extremely messy very.
- 5:25Speaker 1: Quickly, because it's doing too much at once.
- 5:27Speaker 2: Exactly, you are mixing the physical act of extracting data
- 5:31Speaker 2: from the dom with the logic of parsing and structuring it.
- 5:34Speaker 2: If a website changes its layout, your extraction logic and
- 5:38Speaker 2: your structuring logic are completely tangled together.
- 5:41Speaker 1: Ah, I see the problem. If I have fifty fields
- 5:44Speaker 1: to scrape, a manual dictionary approach turns my spider into
- 5:49Speaker 1: a massive, unreadable.
- 5:51Speaker 3: Wall of code.
- 5:52Speaker 2: Precisely, and it's a nightmare to maintain. The loader neatly
- 5:56Speaker 2: encapsulates the extraction process. It holds the raw data secure
- 6:00Speaker 2: surely so that it can be cleanly handed off to
- 6:02Speaker 2: the next stage.
- 6:02Speaker 3: That makes a lot of sense.
- 6:03Speaker 2: And here is the mechanical kicker. The whole loading process
- 6:07Speaker 2: officially concludes when you call a built in method called
- 6:09Speaker 2: load item.
- 6:10Speaker 1: Okay, so, if the loader is the shopper holding the
- 6:12Speaker 1: box and we've mapped out the coordinates and grab the goods,
- 6:15Speaker 1: there has to be a trigger that actually seals the
- 6:16Speaker 1: box and says, hey, this item is finished. Because I'm
- 6:19Speaker 1: assuming we can't just leave the data floating in the
- 6:22Speaker 1: loader's memory forever. Load item must be that trigger.
- 6:26Speaker 2: You hit the nail on the head. Load itom packages
- 6:28Speaker 2: the populated object and hands it back to the framework's
- 6:31Speaker 2: core engine. It essentially takes the finished box out of
- 6:35Speaker 2: the shopper's hands and places it onto a conveyor belt.
- 6:38Speaker 3: A conveyor belt, okay.
- 6:39Speaker 2: Yes, and it also serves as the catalyst that triggers
- 6:43Speaker 2: any pre processing or post processing logic you might have configured.
- 6:48Speaker 2: If you were building this manually with basic Python dictionaries,
- 6:51Speaker 2: you'd have to write all that handoff and processing logic
- 6:54Speaker 2: from scratch every single time.
- 6:57Speaker 1: Which brings us perfectly to the next major problem. Our
- 7:00Speaker 1: personal shopper sealed the box and put it on the
- 7:02Speaker 1: cavera belt, But if we look inside that box, the
- 7:06Speaker 1: raw materials they gathered are probably still flawed. Oh all,
- 7:10Speaker 1: Like a price might have a pound symbol or dollar
- 7:13Speaker 1: sign stuck to it, or a string of text might
- 7:14Speaker 1: have a bunch of weird whitespace characters from the HTML formatting.
- 7:18Speaker 1: If I'm building a database for bargain hunters, saving a
- 7:21Speaker 1: string like pound fifty five point zero zero is completely useless.
- 7:26Speaker 1: Because a computer program cannot execute math on a currency symbol.
- 7:30Speaker 1: We need a way to intercept and mutate that data
- 7:33Speaker 1: in transit exactly.
- 7:34Speaker 2: Structure does not equal cleanliness. The raw materials have been gathered,
- 7:39Speaker 2: but they are absolutely not a finished product ready for
- 7:42Speaker 2: a database. They need to be refined, checked, and validated
- 7:46Speaker 2: in a very specific automated sequence.
- 7:48Speaker 3: So how do we do that?
- 7:49Speaker 2: Enter the item pipeline.
- 7:51Speaker 1: Item pipelines. That implies a literal factory assembly line.
- 7:54Speaker 2: And that is exactly how they function mechanically. Item pipelines
- 7:58Speaker 2: are Python classes that the framework uses to chain together
- 8:02Speaker 2: various transformations, operations, and checks on your raw scraped data.
- 8:06Speaker 3: So it's an automated sequence.
- 8:07Speaker 2: Exactly. Once LOADEDUM yields the data, the framework's engine automatically
- 8:11Speaker 2: routes that item through these pipeline classes, executing them one
- 8:14Speaker 2: after the other.
- 8:15Speaker 1: Let's dig into the mechanics of that. What are we
- 8:17Speaker 1: actually doing to the data inside these pipelines? Give me
- 8:19Speaker 1: some real world use cases.
- 8:21Speaker 2: They are essential for almost every professional scraping scenario. First
- 8:25Speaker 2: is simply cleaning data. You might need a pipeline to
- 8:29Speaker 2: format results, strip out unneeded HTML characters, or remove that
- 8:32Speaker 2: currency symbol you mentioned, so you just have a pure
- 8:35Speaker 2: integer or float to do mathon.
- 8:37Speaker 1: That makes total sense. We need clean data types.
- 8:40Speaker 3: What else?
- 8:41Speaker 2: Validating data is massive. You need a pipeline step to
- 8:45Speaker 2: ensure that the prices you've scraped are actually numerical values
- 8:48Speaker 2: and not just a string of text that says out
- 8:49Speaker 2: of stock.
- 8:50Speaker 3: Well, I hate when that happens, right.
- 8:52Speaker 2: You also use pipelines to check that URLs are structurally valid,
- 8:56Speaker 2: or to verify with a regular expression that something claiming
- 8:59Speaker 2: to be an email address actually contains an at symbol
- 9:02Speaker 2: and a domain.
- 9:03Speaker 1: So it's strict quality control it is.
- 9:06Speaker 2: You also use pipelines to ensure that crucial items actually
- 9:08Speaker 2: contain values and didn't just come back as empty null values.
- 9:12Speaker 2: You can write logic to cross reference data and remove
- 9:15Speaker 2: duplicate entries.
- 9:16Speaker 3: Okay.
- 9:16Speaker 2: Well, and finally, storing the final polished items into external files,
- 9:20Speaker 2: sql databases or cloud storage is the classic final step
- 9:24Speaker 2: of a pipeline.
- 9:25Speaker 1: Let's ground this with a highly specific example. Let's say
- 9:28Speaker 1: we are scraping products. We have our item loader, and
- 9:31Speaker 1: we need to deal with pricing anomalies.
- 9:33Speaker 2: Okay, Let's imagine we build a class called price pipeline.
- 9:36Speaker 2: Every pipeline class requires a specific method called process item.
- 9:40Speaker 2: As the framework routes your data, it passes the item
- 9:44Speaker 2: through this method.
- 9:45Speaker 3: Okay, process item. Got it.
- 9:47Speaker 2: Let's say the website we are scraping lists prices before
- 9:50Speaker 2: a mandatory sales tax inside process item, you can write
- 9:54Speaker 2: a simple rule look at the items price field and
- 9:57Speaker 2: if it doesn't include the sales tax, multiply the float
- 10:00Speaker 2: value by the tax rate to calculate the true total,
- 10:02Speaker 2: and then return the updated item to the conveyor belt.
- 10:05Speaker 1: But wait, what if the personal shop or grabbed a
- 10:08Speaker 1: product and because the target website had a temporary rendering glitch,
- 10:12Speaker 1: the price field is just well empty. It happens a lot, right,
- 10:16Speaker 1: And if you try to multiply a null value or
- 10:18Speaker 1: an empty string by a tax rate, Python is going
- 10:21Speaker 1: to throw a type AAR and your entire SCRAPEG program
- 10:23Speaker 1: is going to crash.
- 10:24Speaker 2: Right, It absolutely would crash, which is why the pipeline
- 10:27Speaker 2: architecture has a built in error handling mechanism called the
- 10:30Speaker 2: drop item exception.
- 10:31Speaker 1: Drop item. Yes, inside your price pipeline, you write a
- 10:34Speaker 1: defensive condition. If an item arrives completely missing a mandatory
- 10:38Speaker 1: price field, you use the command raised drop item.
- 10:42Speaker 2: What does raising that exception actually do mechanically? Does the
- 10:45Speaker 2: program just stop entirely?
- 10:47Speaker 1: No, And that is the beauty of it. The framework
- 10:50Speaker 1: catches that specific exception, it completely halts that single item's
- 10:55Speaker 1: progress down the pipeline.
- 10:56Speaker 3: Oh, just the one item exactly.
- 10:58Speaker 1: It pulls that defective item right off the conveyor belt,
- 11:01Speaker 1: logs it, and discards it so it doesn't crash the
- 11:04Speaker 1: next worker down the line. Meanwhile, all the other healthy
- 11:07Speaker 1: items keep slowing without interruption.
- 11:09Speaker 2: Here's where it gets really interesting. If we extend our metaphor,
- 11:13Speaker 2: the pipeline is the factory floor. Yes, the shopper dumps
- 11:16Speaker 2: the raw materials at the start of the belt as
- 11:19Speaker 2: the item moves down the line. Each worker, meaning each
- 11:23Speaker 2: process item method, has one hyper specific job.
- 11:26Speaker 1: One worker cleans the text, the next worker verifies it's
- 11:30Speaker 1: a real number. The next worker applies the sales tax calculation.
- 11:34Speaker 1: And if any worker detects a critically defective item, they
- 11:37Speaker 1: hit a button that raises the drop item exception, basically
- 11:40Speaker 1: dropping it through a trapdoor before it can ruin the
- 11:42Speaker 1: final shipment.
- 11:43Speaker 2: That is the exact architectural flow you should be visualizing.
- 11:46Speaker 2: It is a sequence of highly specialized, isolated workers.
- 11:50Speaker 1: To truly understand how this machine is constructed, we need
- 11:54Speaker 1: to see it run end to end. Let's trace a
- 11:56Speaker 1: practical example, step by step. Let's talk about scraping a
- 11:59Speaker 1: sandbox site.
- 12:00Speaker 2: A great exercise. Let's use books dot to scrape dot com. Okay,
- 12:04Speaker 2: it's a classic sandbox environment structured into different catalog categories.
- 12:09Speaker 2: It is essentially a mock online bookstore, which makes it
- 12:12Speaker 2: the perfect legal playground to test these extraction tools.
- 12:15Speaker 3: Let's look at the before picture.
- 12:17Speaker 1: Before developer learns about loaders and pipelines, how do they
- 12:20Speaker 1: traditionally try to scrape this bookstore. Let's talk about the
- 12:24Speaker 1: pain of the manual approach.
- 12:25Speaker 2: Oh, the manual approach, Well, you write your basic spider logic,
- 12:29Speaker 2: you set it up to crawl through the catalog pages.
- 12:31Speaker 2: You write your expath selectors to target the book's title,
- 12:34Speaker 2: the price, the cover image URL, and the actual product
- 12:38Speaker 2: page URL.
- 12:39Speaker 1: That sounds like standard targeting, but the pain starts where
- 12:41Speaker 1: you try to.
- 12:42Speaker 2: Save it exactly. To save that data manually, developers typically
- 12:46Speaker 2: set up a four loop to iterate over the extracted elements,
- 12:50Speaker 2: and inside this loop, if they aren't using a framework's
- 12:53Speaker 2: built in export tools, they will often import a Python
- 12:56Speaker 2: library called codex codex.
- 12:59Speaker 1: If you're doing this mannually, you're probably wrestling with Codex
- 13:01Speaker 1: to handle file writing.
- 13:03Speaker 2: Yes, Codex is used for encoding and decoding data, specifically
- 13:07Speaker 2: to handle things like UTF eight character encoding when writing
- 13:10Speaker 2: text files safely. Right, you would manually use codex dot
- 13:14Speaker 2: open to create a file called books dot txt or
- 13:17Speaker 2: books dot csv. Then, line by line inside your loop,
- 13:21Speaker 2: you command your script to write the extracted title, price,
- 13:24Speaker 2: and URLs directly into that file, usually combining them into
- 13:27Speaker 2: a comma separated string.
- 13:29Speaker 1: And the second a book title actually contains a comma
- 13:32Speaker 1: in its text, your entire CSV structure breaks because you
- 13:35Speaker 1: just added an accidental column.
- 13:37Speaker 2: Yes, so frustrating.
- 13:39Speaker 1: Plus you are manually hard coding filepaths, opening file loocks,
- 13:43Speaker 1: and just hoping the script doesn't crash half way through
- 13:45Speaker 1: and corrupt the whole file.
- 13:47Speaker 2: It is incredibly brittle. I mean it works for a
- 13:49Speaker 2: ten item test, sure, but it does not scale to
- 13:52Speaker 2: ten thousand items. If you want to change how the
- 13:54Speaker 2: data is saved or add a step to clean the data,
- 13:58Speaker 2: you have to rewrite your core spider code.
- 14:00Speaker 1: Okay, so let's look at the after picture. How do
- 14:02Speaker 1: we upgrade this manual, brittle clunker into a high performance
- 14:06Speaker 1: machine using the framework tools.
- 14:09Speaker 2: First, we completely delete the manual text file writing. We
- 14:12Speaker 2: throw out the codex logic and the four loops entirely.
- 14:15Speaker 3: Goodbye codex.
- 14:16Speaker 2: Instead, we bring in our personal shopper. We instantiate the
- 14:19Speaker 2: item loader and pass it the selector for the current
- 14:21Speaker 2: book element.
- 14:22Speaker 3: Okay, so the shopper's ready.
- 14:24Speaker 2: Now we introduce a powerful new concept input processors, specifically
- 14:28Speaker 2: a built in processor called map compose.
- 14:31Speaker 1: The map compose what exactly is map composed doing under
- 14:34Speaker 1: the hood.
- 14:34Speaker 2: So, map compose is a processor that allows you to
- 14:37Speaker 2: apply a sequence of functions to your extracted data immediately,
- 14:40Speaker 2: like before it even fully settles into the item object.
- 14:43Speaker 3: Wait immediately, Yes.
- 14:45Speaker 2: Let's look at the URLs we scraped from the sandbox
- 14:48Speaker 2: site on books dot to scrape dot com. The image
- 14:51Speaker 2: URLs and the book URLs are incomplete. They are relative links. Okay,
- 14:55Speaker 2: they look like catalog slash book one dot HTML instead
- 14:59Speaker 2: of a full clickable web address.
- 15:01Speaker 3: Right, they're just fragments.
- 15:02Speaker 1: If you try to open them in a browser or
- 15:03Speaker 1: save them to a database, they lead.
- 15:05Speaker 2: Nowhere, precisely. So outside of our spider, we create a
- 15:09Speaker 2: simple custom Python function called adlink. All this function does
- 15:13Speaker 2: is take a string and concatenate or join the base
- 15:17Speaker 2: URL of the website with the fragmented url. We just
- 15:20Speaker 2: passed it returning a fully qualified functioning weblink.
- 15:23Speaker 3: And we plug this custom function into map compost.
- 15:26Speaker 2: Yes. When we configure our item loader, we tell it
- 15:29Speaker 2: that whenever it grabs a raw URL fragment via XPath,
- 15:32Speaker 2: it must immediately pass that fragment through the map composed processor,
- 15:35Speaker 2: and we pass ad link as the argument. Oh wow,
- 15:37Speaker 2: the processor applies the function, meaning the URL is fixed
- 15:41Speaker 2: and properly formatted before it is ever officially assigned to
- 15:44Speaker 2: our book item. You can even chain them. You could
- 15:47Speaker 2: have map composed, strip white space, then apply ad link
- 15:51Speaker 2: all in one line of code.
- 15:52Speaker 1: That is incredibly slick. But wait, I have a mechanical
- 15:55Speaker 1: question about that.
- 15:56Speaker 2: Go for it.
- 15:57Speaker 1: Why is map composed designed this way? If I target
- 16:00Speaker 1: one single price or one URL on a page, shouldn't
- 16:03Speaker 1: it just return a single string? Why do we need
- 16:05Speaker 1: a special processor to map functions over it?
- 16:08Speaker 2: That is a fantastic catch. It has to do with
- 16:10Speaker 2: how extraction languages work. XPath and CSS selectors are designed
- 16:14Speaker 2: to query the dom and find all matches that fit.
- 16:16Speaker 1: Your coordinates, right, all of them.
- 16:18Speaker 2: Even if you know there is only one price on
- 16:20Speaker 2: the page. The underlying framework engine doesn't know that so
- 16:23Speaker 2: to be safe, it always returns extracted data as a
- 16:26Speaker 2: Python list an iterable array. It returns a list containing
- 16:30Speaker 2: your one item.
- 16:31Speaker 1: Ah, so the data is trapped inside a list format
- 16:34Speaker 1: like bracket quote fifty pounds end quote bracket instead of
- 16:38Speaker 1: just the string fifty.
- 16:38Speaker 2: Pounds exactly, and mac composed is specifically built to iterate
- 16:42Speaker 2: over lists. It applies your function to every element inside
- 16:45Speaker 2: that list. Okay, that makes sense, so you still have
- 16:47Speaker 2: a list at the end of it. This is why
- 16:48Speaker 2: we must also assign an output processor called take first
- 16:51Speaker 2: to our loader.
- 16:52Speaker 1: Let me guess takes first, looks at the list, grabs
- 16:56Speaker 1: the very first element out of it, and discards the
- 16:59Speaker 1: list's structure entirely.
- 17:00Speaker 2: You've got it. It extracts the clean string value right
- 17:03Speaker 2: out of the array.
- 17:04Speaker 1: So to summarize the immense benefit for you listening, by
- 17:08Speaker 1: deleting our manual codex loop and plugging into map compose
- 17:11Speaker 1: and take first via the item loader, we are leveraging
- 17:14Speaker 1: the frameworks Optimize internal engine exactly. We aren't reinventing the wheel.
- 17:18Speaker 1: It makes your code modular, highly scalable, and the resulting
- 17:21Speaker 1: items are so well structured that you can just tell
- 17:23Speaker 1: the framework to output them directly to ADJSON or CSV
- 17:27Speaker 1: file with a single command line flag, without writing a
- 17:31Speaker 1: single line of file saving code.
- 17:33Speaker 2: It lets the framework handle all the heavy lifting of
- 17:35Speaker 2: state management and file locking, freeing you up to focus
- 17:38Speaker 2: entirely on the data logic.
- 17:40Speaker 1: Which means we can finally move to the advanced logic.
- 17:44Speaker 1: Our URLs are fixed, our strings are pulled out of
- 17:46Speaker 1: their lists, our data is structured. Let's use the pipelines
- 17:50Speaker 1: to execute some actual business rules. Let's return to our
- 17:54Speaker 1: scenario where we're building a tool for bargain hunters. We
- 17:58Speaker 1: only want to find and process affordable books.
- 18:01Speaker 2: A perfect real world application, Let's say we want to
- 18:04Speaker 2: analyze the prices and anything that costs over fifty we
- 18:07Speaker 2: want to flag it as expensive, so our downstream systems
- 18:10Speaker 2: know exactly how to categorize it.
- 18:12Speaker 3: How do we architect that sequence?
- 18:14Speaker 2: First, we rely on the cleaning task we mentioned earlier.
- 18:17Speaker 2: The price we scraped definitely has a pound symbol attached
- 18:20Speaker 2: to it. A computer cannot evaluate if a symbol is
- 18:22Speaker 2: greater than fifty, so we already have an input processor
- 18:25Speaker 2: stripping out the parent symbol and converting the string to
- 18:28Speaker 2: a float. So we have a pure number.
- 18:29Speaker 3: Okay, so the data is prepped.
- 18:31Speaker 2: Next, we set up two entirely separate pipeline classes in
- 18:34Speaker 2: our project to handle our business logics sequentially, and this
- 18:37Speaker 2: sequence is strictly managed in a central configuration file called
- 18:41Speaker 2: settings dot pi.
- 18:42Speaker 1: Two pipelines. I see where this is going. But how
- 18:45Speaker 1: does settings dot pi actually know which one to run first?
- 18:48Speaker 2: Inside Settings dot pi there is a configuration dictionary specifically
- 18:52Speaker 2: for item pipelines. The keys are the names of your
- 18:55Speaker 2: pipeline classes, and the values are priority numbers, typically ranging
- 19:00Speaker 2: from zero to a thousand. Oh I see the framework's
- 19:02Speaker 2: engine reads this dictionary. The lower the number, the earlier
- 19:06Speaker 2: that pipeline runs on the assembly line.
- 19:08Speaker 1: So it acts as the exact schedule for the factory floor.
- 19:11Speaker 1: Walk me through the data flow here.
- 19:12Speaker 2: Our first pipeline class is called books price. In settings
- 19:15Speaker 2: dot pi, we map it to a priority value of
- 19:17Speaker 2: one hundred. Okay, because one hundred is the lowest number
- 19:20Speaker 2: in our setup. The engine routes the data there first.
- 19:23Speaker 2: Its processidea method has a simple job. It evaluates the
- 19:26Speaker 2: pure number price. If the float value is strictly greater
- 19:29Speaker 2: than fifty, it overwrites the item's price field entirely with
- 19:33Speaker 2: the text string expensive got it if it's fifty or below.
- 19:36Speaker 2: It leaves the price field exactly as it is.
- 19:38Speaker 1: So books price at priority one hundred is essentially the
- 19:40Speaker 1: labeling worker. It slaps an expensive sticker on the pricey
- 19:43Speaker 1: books and passes them along exactly.
- 19:46Speaker 2: Then the framework takes the item and routes it down
- 19:49Speaker 2: the belt to our second pipeline class, which we will
- 19:52Speaker 2: call checks is viable In our settings dictionary. We map
- 19:56Speaker 2: this to a priority value of two hundred. It fundamentals
- 20:00Speaker 2: executes after pipeline one because.
- 20:02Speaker 1: The engine sees that two hundred is numerically higher than
- 20:04Speaker 1: one hundred.
- 20:05Speaker 2: Right inside checks viable, the processcytem method looks out the
- 20:09Speaker 2: price field. It checks to see if the field literally
- 20:12Speaker 2: contains the string expensive. Okay, If it does not contain it,
- 20:16Speaker 2: meaning it's an affordable book that passed through the first
- 20:18Speaker 2: worker unchanged, it executes a command to print the book's
- 20:21Speaker 2: details to the console for us to verify.
- 20:24Speaker 1: So what does this all mean? I understand the priority
- 20:27Speaker 1: numbers completely lower runs first, higher runs later, But I
- 20:30Speaker 1: have to challenge the architecture here. Bring on, why split
- 20:32Speaker 1: this into two different classes with numbers one hundred and
- 20:35Speaker 1: two hundred. Why not just have one single pipeline worker
- 20:38Speaker 1: at priority one hundred who checks if the float is
- 20:41Speaker 1: over fifty, and if it's not, just prints it out
- 20:44Speaker 1: right then and there. Yeah, writing two classes seems like
- 20:46Speaker 1: over engineering.
- 20:47Speaker 2: It might seem that way at first glance. But if
- 20:49Speaker 2: we connect this to the bigger picture of software architecture,
- 20:52Speaker 2: separating these concerns into modular pipelines is what saves you
- 20:55Speaker 2: from technical debt. How so you have one pipeline strictly
- 20:59Speaker 2: dedicated to data labeling and one entirely separate pipeline strictly
- 21:03Speaker 2: dedicated to filtering. This makes your code infinitely reusable.
- 21:07Speaker 1: How does that actually play out in the long run, though?
- 21:09Speaker 2: Imagine if next month project requirements change, you decide you
- 21:14Speaker 2: don't just want to hide the expensive books from the
- 21:16Speaker 2: console output, you want to permanently delete them from your
- 21:19Speaker 2: data set using that drop item exception we analyzed earlier. Ah,
- 21:23Speaker 2: I see, Or maybe you want to take only the
- 21:25Speaker 2: affordable data and write it to a post reschool database.
- 21:28Speaker 2: Because your architecture is modular, you don't have to touch
- 21:32Speaker 2: or risk breaking your core labeling logic.
- 21:34Speaker 1: You just leave the one hundred pipeline alone exactly.
- 21:37Speaker 2: You just write a new isolated database pipeline class, slot
- 21:40Speaker 2: it into the settings dot pi dictionary with a priority
- 21:43Speaker 2: of say three hundred, and you're done. Modularity gives you
- 21:47Speaker 2: immense scaling flexibility.
- 21:49Speaker 1: I completely see the vision. Now you're building a factory
- 21:53Speaker 1: that is effortless to reconfigure. Let's do a quick recap
- 21:57Speaker 1: of the architectural journey we just mapped out. We start
- 22:00Speaker 1: with the absolute chaos of raw web.
- 22:02Speaker 2: Markup the messy pile YEP.
- 22:05Speaker 1: We brought in our personal shopper, the item loader, to
- 22:07Speaker 1: extract exactly what we needed while separating our logic. We
- 22:11Speaker 1: used input processors like map compose to fix broken relative
- 22:15Speaker 1: URLs on the fly and take first to rescue our
- 22:18Speaker 1: data from XPath.
- 22:19Speaker 2: Lists beautifully summarized.
- 22:21Speaker 1: And finally, we routed our clean items through a sequential
- 22:24Speaker 1: pipeline assembly line managed by settings dot pi to evaluate numbers,
- 22:27Speaker 1: label expensive items, and filter the final output based on
- 22:31Speaker 1: strict priority execution.
- 22:33Speaker 2: It is a phenomenal engineering transformation. You take a messy,
- 22:36Speaker 2: unusable pile of HTML on the floor and automatically forge
- 22:39Speaker 2: it into a pristine, perfectly validated data set, ready for
- 22:43Speaker 2: any application and.
- 22:44Speaker 1: To reinforce everything we unravel today, I want to leave
- 22:46Speaker 1: you with a short mental exercise to test your understanding
- 22:49Speaker 1: of the architecture. Think back to that dictionary in the
- 22:51Speaker 1: settings dot PI file book sprice our labeling worker was
- 22:54Speaker 1: assigned priority one hundred, checks viable our printing worker with
- 22:58Speaker 1: priority two hundred. If you wanted to architect a brand
- 23:01Speaker 1: new pipeline class that completely deletes the expense of books
- 23:06Speaker 1: using the raise drop item exception, what exact priority number
- 23:10Speaker 1: would you assign it in the dictionary to ensure it
- 23:12Speaker 1: runs after the books are evaluated and labeled, but before
- 23:15Speaker 1: the final viable books are printed out.
- 23:17Speaker 2: That is a phenomenal puzzle. Understanding exactly where to place
- 23:21Speaker 2: that new worker on the assembly line is where the
- 23:23Speaker 2: true power of this framework unlocks.
- 23:26Speaker 1: Think about the numerical sequence. We will leave you to
- 23:28Speaker 1: map that out on your own. Thanks for joining us
- 23:31Speaker 1: for this deep dive into architecting and elegant bulletproof data extraction.
- 23:35Speaker 2: Machine, happy building, and keep your pipelines clean.
- 23:38Speaker 3: Well. Gats you next time.