Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 28: Base and Generic Crawling Classes
Transcript
- 0:00Speaker 1: Right at this exact millisecond, there are millions of invisible
- 0:05Speaker 1: digital agents just silently traversing fiber optic cables all around
- 0:09Speaker 1: the globe. Oh.
- 0:09Speaker 2: Absolutely, they're constantly mapping network infrastructure or you know, indexing
- 0:14Speaker 2: the sum total of human knowledgy, yeah.
- 0:15Speaker 1: And screenping these massive data sets to feed the next
- 0:19Speaker 1: generation of AI models.
- 0:21Speaker 2: Right. The scale of this automated extraction is honestly staggering.
- 0:26Speaker 2: When we navigate the modern information landscape today, we aren't
- 0:29Speaker 2: talking about like human hands on keyboards anymore.
- 0:31Speaker 1: Definitely not the whole manual process of browsing and copying
- 0:35Speaker 1: and pasting. That's completely obsolete.
- 0:38Speaker 2: Exactly. We are dealing with the deployment of autonomous fleets
- 0:41Speaker 2: that operate at computational speeds.
- 0:43Speaker 1: Which brings us to today's topic. Welcome to the deep dive. Today,
- 0:47Speaker 1: we are going to visualize and master the underlying architecture
- 0:50Speaker 1: of web automation using Python's screepy library. Specifically, we're focusing
- 0:55Speaker 1: on spiders, right, and not the creepy Crawley kind. We
- 0:58Speaker 1: mean the highly specialized digital tools engineered to fetch massive
- 1:01Speaker 1: amounts of data from the Internet.
- 1:03Speaker 2: And I think it's important to note that understanding what
- 1:05Speaker 2: tools are available is just as critical as knowing why
- 1:09Speaker 2: we use them to navigate all this information.
- 1:11Speaker 1: Overload, for sure, and since we are building mental models today,
- 1:15Speaker 1: you don't need a screen. We are doing a purely audio,
- 1:19Speaker 1: step by step walkthrough of how these tools operate under
- 1:23Speaker 1: the hood, which.
- 1:24Speaker 2: Is a great way to learn it, honestly, because understanding
- 1:26Speaker 2: the structural framework of these tools gives you a distinct
- 1:29Speaker 2: advantage whether you're, say, building pipelines for cybersecurity auditing, or
- 1:34Speaker 2: training large language models.
- 1:35Speaker 1: Or engineering massive data aggregation platforms.
- 1:38Speaker 2: Right exactly, Mastering the scrapey architecture allows you to transform
- 1:42Speaker 2: the chaotic unstructured web into normalized data sets.
- 1:46Speaker 1: So before we instantiate a single Python class or pick
- 1:51Speaker 1: a specific tool, we have to define what a spider
- 1:54Speaker 1: actually is in this context.
- 1:55Speaker 2: Right, in this scrapey ecosystem, a spider isn't the entire
- 1:58Speaker 2: scraping operation. It's actually a very specific Python class where
- 2:02Speaker 2: custom behaviors are defined.
- 2:04Speaker 1: Because the underlying scrapy engine handles all the heavy lifting,
- 2:07Speaker 1: right like the asynchronous network requests the concurrency the scheduling.
- 2:12Speaker 2: That's right, the spider is simply the behavioral blueprint that
- 2:15Speaker 2: you hand over to the engine. It just dictates the
- 2:18Speaker 2: logic for crawling a domain and parsing its pages. They
- 2:21Speaker 2: are organized structures that make it easy for the engine
- 2:24Speaker 2: to retrieve content.
- 2:25Speaker 1: Okay, let's unpack this. If I'm trying to visualize the
- 2:28Speaker 1: scrapy engine's relationship with the spider, I kind of think
- 2:30Speaker 1: of it like sending an assistant to a massive library.
- 2:33Speaker 1: Oh I like that, right, So the engine needs three
- 2:36Speaker 1: fundamental instructions from the spider to function effectively. The first
- 2:39Speaker 1: is the scope. So that's like telling your assistant exactly
- 2:43Speaker 1: which building and floor to go to.
- 2:45Speaker 2: Yes, exactly. In the code, this scope is defined by
- 2:48Speaker 2: understanding what can be crawled. Usually you specify the exact
- 2:52Speaker 2: URLs in a method called start requests.
- 2:55Speaker 1: Got it. So without that starting line, the engine has
- 2:57Speaker 1: no coordinates to initiate.
- 2:59Speaker 2: Anything precisely, the engine needs those initial coordinates to kick
- 3:03Speaker 2: off the asynchronous HTTP requests.
- 3:06Speaker 1: Okay, So once the scope is defined, the engine needs
- 3:09Speaker 1: to know the crawling mechanism right.
- 3:11Speaker 2: Right, which is understanding how it will be crawled.
- 3:14Speaker 1: So, going back to my assistant analogy, the mechanism is
- 3:17Speaker 1: telling them whether to just read the book they find,
- 3:20Speaker 1: or to actually follow the footnotes to go find other
- 3:23Speaker 1: books in the library.
- 3:24Speaker 2: That's a perfect way to put it. You are programming
- 3:27Speaker 2: the logic for discovering new URLs. The spider tells the
- 3:32Speaker 2: engine whether it should follow links on the page or
- 3:35Speaker 2: just look at one specific portion makes sense.
- 3:37Speaker 1: And then the third fundamental aspect is the parsing.
- 3:40Speaker 2: Yes, understanding how the data is extracted.
- 3:44Speaker 1: So telling the library assistant to only write down the
- 3:46Speaker 1: text that's in bold, for example, does that accurately describe
- 3:50Speaker 1: the relationship there?
- 3:51Speaker 2: It absolutely does. The web is inherently unstructured, so parsing
- 3:55Speaker 2: is where you apply specific rules, like telling it exactly
- 3:58Speaker 2: what parts of the page are processed and next distracted
- 4:00Speaker 2: for post processing.
- 4:01Speaker 1: Okay, so we have our three instructions scope, crawling, mechanism,
- 4:05Speaker 1: and parsing. Now let's talk about the most foundational tool
- 4:09Speaker 1: Scripey offers to execute them right.
- 4:11Speaker 2: The base class. It's literally just called screepy dot spider, and.
- 4:14Speaker 1: It's basically a blank canvas, isn't.
- 4:16Speaker 2: It It is it's the simplest category, and because it's
- 4:18Speaker 2: the base class, every other spider must inherit from it.
- 4:21Speaker 1: Whether it's one bundled with scrapey or a custom one
- 4:25Speaker 1: you write yourself exactly.
- 4:26Speaker 2: It provides a default start request method that just sends
- 4:29Speaker 2: requests from a start earl's attribute, and then it calls
- 4:32Speaker 2: a parse method for the responses.
- 4:34Speaker 1: Let's walk through a mental visualization of building this. So first,
- 4:38Speaker 1: obviously you import the scrapee library right.
- 4:41Speaker 2: Then you define the name, which is just the spider's
- 4:44Speaker 2: unique identifier, and.
- 4:45Speaker 1: Then critically, you establish your allowed domains.
- 4:49Speaker 2: Yes, that is so important. It's a list that keeps
- 4:52Speaker 2: the spider from wandering off site.
- 4:53Speaker 1: Because setting aloud domains isn't just about focus, right. The
- 4:57Speaker 1: web is full of spider traps.
- 4:58Speaker 2: Oh, completely infinitely loops of dynamically generated URLs right.
- 5:03Speaker 1: Or interconnected domains. If you forget to configure this hard boundary,
- 5:08Speaker 1: a single outbound link on your target site could send
- 5:11Speaker 1: your scraper careening into external servers and.
- 5:14Speaker 2: Suddenly your local machine is trying to index the entire Internet.
- 5:17Speaker 2: You would saturate your bandwidth and trigger automated IP bands
- 5:21Speaker 2: almost immediately.
- 5:22Speaker 1: Wow. Yeah, so the engine absolutely needs that whitelist. And
- 5:25Speaker 1: then after that you define your start rolls, which is
- 5:28Speaker 1: where the crawling actually begins right.
- 5:29Speaker 2: And finally you define the parse method which processes the
- 5:32Speaker 2: downloaded responses.
- 5:34Speaker 1: So inside that parse method, you could say, iterate through
- 5:37Speaker 1: all the H one tags on a page to get
- 5:40Speaker 1: values using.
- 5:41Speaker 2: The XPath method exactly, and then you could iterate through
- 5:44Speaker 2: the A tags to get all the available links. And
- 5:47Speaker 2: what's cool is it can ultimately return multiple requests and
- 5:50Speaker 2: items from just a single callback.
- 5:53Speaker 1: Which brings up a question for me. If this base
- 5:55Speaker 1: spider doesn't provide any special functionality on its own, like
- 5:59Speaker 1: as it's just a blank canvas, why is it the
- 6:02Speaker 1: most commonly.
- 6:02Speaker 2: Used Well comes down to control.
- 6:04Speaker 1: Is it simply because developers prefer a blank canvas where
- 6:07Speaker 1: they can manually define the exact xpaths and tags rather
- 6:11Speaker 1: than being boxed in.
- 6:12Speaker 2: That's exactly it. The value of flexibility is huge. Writing
- 6:17Speaker 2: customized logic inside the base spider makes perfect sense for
- 6:21Speaker 2: navigating standard chaotic HTML documents. You have total granular control
- 6:27Speaker 2: over the extraction.
- 6:28Speaker 1: Here's where it gets really interesting though, because while a
- 6:32Speaker 1: blank canvas is great, sometimes you don't want to reinvent
- 6:34Speaker 1: the wheel.
- 6:35Speaker 2: Right. If you have a highly specific, standardized task, building
- 6:39Speaker 2: from scratch is just inefficient.
- 6:41Speaker 1: You need a pre built tool. So let's pivot to
- 6:43Speaker 1: data structures that break the standard web paradigm. Like, what
- 6:46Speaker 1: if you're targeting a massive online data set that is
- 6:49Speaker 1: just a giant spreadsheet.
- 6:51Speaker 2: Yeah, like a monolithic XML feed or massive CSV file.
- 6:55Speaker 2: In those cases, you use Crapy's generic spiders.
- 6:57Speaker 1: These are designed for those common scraping use cases exactly.
- 7:01Speaker 2: For example, the XML feed spider, it's specifically designed to
- 7:04Speaker 2: parse XML feeds by iterating through node names.
- 7:07Speaker 1: Rather than trying to load the entire document into memory
- 7:10Speaker 1: at once.
- 7:10Speaker 2: Right, because if you apply a base bider to a
- 7:12Speaker 2: fifty gigabyte file, the engine will try to build a
- 7:15Speaker 2: complete dom tree in RAM.
- 7:16Speaker 1: Which would immediately trigger an out of memory exception.
- 7:19Speaker 2: Boom, your pipeline crashes. So the XML feed spider reads
- 7:23Speaker 2: the stream sequentially and discards nodes as it goes.
- 7:26Speaker 1: That is so smart, and I'm guessing the CSF feed
- 7:29Speaker 1: spider works similarly yep.
- 7:32Speaker 2: It's designed for iterating through rows rather than nodes. You
- 7:34Speaker 2: just give it the delimiter and it executes a callback
- 7:37Speaker 2: for every single row without overloading the system.
- 7:39Speaker 1: So if I'm targeting that massive spreadsheet data set, I
- 7:43Speaker 1: shouldn't waste time programming a base spider to look for
- 7:46Speaker 1: HTM tags, I should just deploy the CSD feed spider
- 7:49Speaker 1: to immediately start reading rows.
- 7:51Speaker 2: Exactly.
- 7:52Speaker 1: It's about picking the right heavy machinery for the specific.
- 7:54Speaker 2: Terrain, perfectly said. And there's one more generic spider worth
- 7:58Speaker 2: mentioning here, which is the site map spider.
- 8:00Speaker 1: Oh right, because sometimes the site is standard HTML, but
- 8:03Speaker 1: it's just an absolute labyrinth.
- 8:05Speaker 2: Yes, sprawling e commerce domains with millions of pages and
- 8:08Speaker 2: terrible internal linking. The sitemap spider allows you to crawl
- 8:11Speaker 2: sites by discovering URLs via their site.
- 8:14Speaker 1: Maps, including nested site maps.
- 8:15Speaker 2: Right. It can discover sitemap URLs directly from the robots
- 8:19Speaker 2: dot txt file, so you bypass the messy page level
- 8:23Speaker 2: HTML navigation entirely.
- 8:25Speaker 1: Okay, so those are amazing for specific file types XML, CSV,
- 8:29Speaker 1: site maps. But what if we face a sprawling, interconnected
- 8:34Speaker 1: network of standard HTML that doesn't have a reliable map.
- 8:38Speaker 2: Ah, then you need the heavy lifter.
- 8:40Speaker 1: Right, what is the best pre built tool for crawling
- 8:43Speaker 1: regular complex websites?
- 8:45Speaker 2: That would be the crawl spider. It's the go to
- 8:48Speaker 2: subclass for regular websites because it provides a highly convenient
- 8:51Speaker 2: mechanism for following links based on a set of defined rules.
- 8:55Speaker 1: Okay, let's break down the properties of this cross spider.
- 8:57Speaker 1: You mentioned rules.
- 8:58Speaker 2: Yes, rules are a list of one or more rule
- 9:01Speaker 2: objects that define the crawling behavior.
- 9:03Speaker 1: And it also has a parse start earl method right.
- 9:06Speaker 2: Exactly, which is called for the start url responses yeah
- 9:09Speaker 2: and returns an item, a request or an interval. But
- 9:12Speaker 2: the real magic is the link extractor right. The link extractor,
- 9:15Speaker 2: it's an object that defines exactly how links are extracted
- 9:17Speaker 2: from each crawl page. It generates a request containing the
- 9:21Speaker 2: links text.
- 9:22Speaker 1: Let's do a mental walkthrough of how you'd implement this
- 9:24Speaker 1: in the code. So you import scrapeye, you import crawl spider,
- 9:27Speaker 1: rule and link extractor.
- 9:29Speaker 2: Right. You define your standard name, allowed domains, and start earls,
- 9:33Speaker 2: and then.
- 9:33Speaker 1: You set a rule. So, for example, you could use
- 9:36Speaker 1: a link extractor looking specifically for URL's containing product dot php, yep, and.
- 9:43Speaker 2: When that URL is found, it triggers a parse item.
- 9:45Speaker 1: Method and inside that method you create a new scrapey
- 9:50Speaker 1: item object. You assign the product name to the name
- 9:53Speaker 1: property and maybe assign the meta value of the link
- 9:56Speaker 1: text to the link text property, and then return the object.
- 9:59Speaker 2: That's the exact implementation structure. Yes.
- 10:01Speaker 1: Wait, you mentioned something earlier about the rules property that
- 10:04Speaker 1: I want to push back on. I'm sure you said
- 10:06Speaker 1: that if multiple rules match the same link, only the
- 10:09Speaker 1: first one is used according to the order they are defined.
- 10:12Speaker 2: Yes, that is correct. It evaluates sequentially.
- 10:14Speaker 1: That sounds like rule ordering is incredibly critical. I mean,
- 10:17Speaker 1: if you put a broad rule at the top of
- 10:19Speaker 1: your list, you might accidentally trigger the wrong extraction and
- 10:22Speaker 1: skip your highly specific rules entirely. Yes, So what does
- 10:26Speaker 1: this all mean for how carefully we have to plan
- 10:29Speaker 1: our logic before we even write the code.
- 10:31Speaker 2: What's fascinating here is that you've hit on this single
- 10:34Speaker 2: most common point of failure for developers using this tool.
- 10:38Speaker 1: Really, just the order of the rule, just the order.
- 10:41Speaker 2: Designing a cross spider is essentially designing a strict filtering
- 10:45Speaker 2: funnel because of that, sequential evaluation. Logical sequencing is absolutely essential.
- 10:51Speaker 1: So if I have a broad catchal rule at index
- 10:53Speaker 1: zero just to map the site, it swallows.
- 10:55Speaker 2: The url the engine applies that broad rule stops evaluating
- 10:59Speaker 2: the rest of the list and your highly specific product
- 11:02Speaker 2: extraction rules sitting below. It never even sees the link.
- 11:06Speaker 1: Oh wow, So you could end up downloading thousands of
- 11:08Speaker 1: pages and returning a completely empty data set just because
- 11:11Speaker 1: of the order of your list.
- 11:12Speaker 2: Exactly, you must place your narrowest, most specific edge cases
- 11:17Speaker 2: at the very top and your broadcatch all rules at
- 11:20Speaker 2: the absolute bottom.
- 11:21Speaker 1: That is such a critical detail. So we've covered the
- 11:24Speaker 1: entire operational spectrum of web automation today.
- 11:26Speaker 2: We really have.
- 11:27Speaker 1: We started by understanding the three core aspects of a
- 11:30Speaker 1: spider its scope, its mechanism, and its parsing.
- 11:34Speaker 2: We built from the base scrapey dot spider and looked
- 11:37Speaker 2: at its flexibility.
- 11:38Speaker 1: Then we utilize the generic tools. The generic spiders for XML,
- 11:43Speaker 1: CSV and site.
- 11:44Speaker 2: Maps save massive amounts of compute and memory.
- 11:46Speaker 1: Yeah, and finally we unleashed the rule based crawl spider
- 11:50Speaker 1: to automate heavy reb navigation.
- 11:52Speaker 2: These tools really do save massive amounts of time and
- 11:55Speaker 2: manual effort for programmers and data enthusiasts.
- 11:58Speaker 1: They are literal superpowers. But before we wrap up, I
- 12:01Speaker 1: want to leave you the listener with a broader technical
- 12:04Speaker 1: question to ponder.
- 12:05Speaker 2: Yeah, let's hear it.
- 12:07Speaker 1: If a crawl spider can be perfectly programmed to blindly
- 12:10Speaker 1: follow rules and extract text, what happens to our data
- 12:13Speaker 1: integrity when the underlying architecture of a website secretly changes
- 12:18Speaker 1: its HTML structure overnight.
- 12:20Speaker 2: Oh that is the nightmare scenario, right.
- 12:22Speaker 1: It really highlights the fragility of web scraping and the
- 12:25Speaker 1: need for constant, ongoing maintenance.
- 12:27Speaker 2: Of your code. Absolutely, you can build the perfect pipeline,
- 12:31Speaker 2: but if the target changes its CSS classes, your perfectly
- 12:34Speaker 2: extracted data sets suddenly turns to garbage.
- 12:36Speaker 1: Exactly something to think about now. To reinforce everything we've
- 12:39Speaker 1: talked about before we go, let's test your memory.
- 12:41Speaker 2: Well, pop quiz yep.
- 12:43Speaker 1: If you are sitting down right now to write a
- 12:45Speaker 1: brand new scrapey spider, can you name the three fundamental
- 12:49Speaker 1: aspects you must define before you start?
- 12:52Speaker 2: Give a second, got your answer?
- 12:54Speaker 1: Remember you need to define its scope, its crawling mechanism,
- 12:57Speaker 1: and how it handles parsing.
- 13:00Speaker 2: Those three and you control the pipeline.
- 13:01Speaker 1: Well said. Thanks for joining us on this deep dive,
- 13:04Speaker 1: keep building, keep exploring, and we'll see you next time.