Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 8: Mastering HTTP and Python Client Libraries
Transcript
- 0:00Speaker 1: Every single time you type a web address into your browser,
- 0:04Speaker 1: you know, and hit enter, you are basically initiating this silent,
- 0:08Speaker 1: completely invisible conversation.
- 0:10Speaker 2: Right and in that split second, you're often handing over
- 0:12Speaker 2: just a terrifying amount of metadata about yourself exactly.
- 0:16Speaker 1: So welcome to today's deep dive. Today we aren't just
- 0:19Speaker 1: going to like, look at that invisible conversation. We're actually
- 0:22Speaker 1: going to teach you how to write a script that
- 0:24Speaker 1: hijacks it.
- 0:25Speaker 2: Yeah, because most people, I mean, we treat the Internet
- 0:27Speaker 2: like a magic trick, right, You click a link and poof,
- 0:30Speaker 2: a page appears. But underneath that whole illusion, there is
- 0:34Speaker 2: a very rigid text based negotiation happening between your device
- 0:40Speaker 2: and some servers you know, somewhere else in the world.
- 0:43Speaker 2: And if you learn the rules of that negotiation, you
- 0:45Speaker 2: can completely change how you interact with the digital landscape.
- 0:49Speaker 1: Which brings us to our actual mission today, webscraving. We
- 0:52Speaker 1: want to transform you from you know, someone who just
- 0:55Speaker 1: manually reads web pages into someone who can peek behind
- 0:58Speaker 1: the curtain and automate data gathering.
- 1:00Speaker 2: Right, because think about how major retailers operate. They change
- 1:03Speaker 2: their prices like millions of times a day based on
- 1:07Speaker 2: competitor data.
- 1:08Speaker 1: Oh or travel aggregators constantly monitoring flight costs.
- 1:12Speaker 2: Exactly, they are paying humans to just sit there and
- 1:15Speaker 2: click refresh on a browser. They're building systems to request
- 1:19Speaker 2: the data, pick it up, and analyze it automatically.
- 1:22Speaker 1: It's kind of like ordering at a restaurant, right Like
- 1:25Speaker 1: you look at the menu, which is your browser, you
- 1:27Speaker 1: give your order to the waiter, which is the request,
- 1:29Speaker 1: and then you just wait for the kitchen the server
- 1:32Speaker 1: to hand you your meal.
- 1:33Speaker 2: I love that analogy.
- 1:34Speaker 1: It's spot on, and today I am so excited because
- 1:37Speaker 1: we're basically sneaking into that kitchen to see how the
- 1:39Speaker 1: food is actually made.
- 1:40Speaker 2: We are and to do that, to scrape the web effectively,
- 1:44Speaker 2: you first have to understand the language that the kitchen
- 1:47Speaker 2: or the web actually speaks, which is HTTP or Hypertext
- 1:51Speaker 2: Transfer Protocol.
- 1:52Speaker 1: Okay HTTP.
- 1:53Speaker 2: Yeah, it's the foundational client server protocol of the Internet.
- 1:56Speaker 1: So let's break that down starting with that client server.
- 2:00Speaker 1: When I hear the word client, my brain immediately defaults
- 2:04Speaker 1: to a web browser like Chrome, Firefox, Safari, whatever. Sure,
- 2:07Speaker 1: that's the most common one, but if we're talking about
- 2:09Speaker 1: automating things, a client could just be like a few
- 2:12Speaker 1: lines of Python code running in my terminal. Right, that's
- 2:14Speaker 1: really just whatever software is initiating the conversation, that is the.
- 2:17Speaker 2: Core of it. Yes, the server, which is where the
- 2:20Speaker 2: website's content actually lives, running on software like Apache and
- 2:25Speaker 2: Jing's or maybe Microsoft's Ice. It doesn't inherently care if
- 2:29Speaker 2: a human is looking at a screen or if a
- 2:32Speaker 2: headless Python script is making the request.
- 2:34Speaker 1: It just takes the order, right.
- 2:36Speaker 2: It simply receives an HTTP request and sends back a response. However,
- 2:40Speaker 2: the nature of that response is incredibly important for anyone
- 2:44Speaker 2: trying to scrape data. Specifically, you need to know whether
- 2:46Speaker 2: the content is static or dynamic.
- 2:48Speaker 1: Oh right, I really want to focus on that distinction
- 2:51Speaker 1: because it seems like a massive trap for beginners.
- 2:53Speaker 2: Oh it is, it really is.
- 2:54Speaker 1: So. Static content means the server sends over plane HTML
- 2:58Speaker 1: and CSS files and they're fixed, right, what you see
- 3:01Speaker 1: in the code is exactly what you get. But dynamic
- 3:04Speaker 1: content means the browser receives the code, but then it
- 3:07Speaker 1: actually has to execute JavaScript to fetch the data and
- 3:10Speaker 1: build the page on.
- 3:11Speaker 2: The fly, which is a critical hurdle. The foundational fetching
- 3:14Speaker 2: tools we're exploring today. They're designed to grab the initial
- 3:17Speaker 2: response from the server. Okay, So if you point a
- 3:20Speaker 2: basic Python script at a heavily dynamic JavaScript rendered web page,
- 3:24Speaker 2: your script will download the raw HTML template, but the
- 3:28Speaker 2: actual data, you know, the product prices, the news articles,
- 3:31Speaker 2: they just won't be there.
- 3:32Speaker 1: Oh wow, because it's not a full browser exactly.
- 3:35Speaker 2: The script acts as the client, but because it doesn't
- 3:38Speaker 2: execute JavaScript, you end up capturing a completely blank digital canvas.
- 3:43Speaker 1: That sounds incredibly frustrating, like you'd spend hours debugging your
- 3:47Speaker 1: code only to realize the data simply wasn't in that
- 3:49Speaker 1: initial delivery at all.
- 3:50Speaker 2: It happens all the time.
- 3:52Speaker 1: So assuming we are targeting a static page, how do
- 3:55Speaker 1: we actually ask for it? I mean, if HTTP is
- 3:58Speaker 1: the language, what are the actual verbs? How do we
- 4:01Speaker 1: ask for different things? I assume there's this specific command
- 4:04Speaker 1: just for reading a page.
- 4:05Speaker 2: There is, and these commands they're known as HTTP methods.
- 4:09Speaker 2: The most ubiquitous one is the get request. Okay, gent, Yeah,
- 4:13Speaker 2: when your browser retrieves news article or an image, it's
- 4:16Speaker 2: making a get request. You are simply fetching a resource
- 4:19Speaker 2: from a target URL without altering anything on the server.
- 4:23Speaker 1: Okay, so get reads data. Yeah, but what about forms
- 4:26Speaker 1: Like if I'm uploading a file or submitting a log
- 4:29Speaker 1: in form. I'm sending information out.
- 4:31Speaker 2: Right right, And in those scenarios you are creating or
- 4:34Speaker 2: updating a resource on the server, and that typically utilizes
- 4:37Speaker 2: a post request. You're packaging your data like your username
- 4:40Speaker 2: and password and basically posting it to the server for processing.
- 4:43Speaker 1: Gotcha. Now, I've also seen the put method mentioned when
- 4:46Speaker 1: reading up on this. But if post and put both
- 4:50Speaker 1: send data to the server, why do we even need
- 4:53Speaker 1: two different commands?
- 4:54Speaker 2: That's a great question. It actually comes down to a
- 4:56Speaker 2: crucial computing concept called idempotency impency. Yeah, the operation is idempatent
- 5:01Speaker 2: if doing it once produces the exact same result as
- 5:03Speaker 2: doing it one hundred times. Oh okay, put is explicitly idempatent.
- 5:08Speaker 2: Think of it like setting a smart thermostat. If you
- 5:10Speaker 2: send a command that says, uh, set temperature to seventy
- 5:15Speaker 2: two degrees, right, Sending that exact same command twenty times
- 5:18Speaker 2: won't make the room any hotter. The end state remains
- 5:21Speaker 2: seventy two degrees I see.
- 5:23Speaker 1: Whereas pist isn't necessarily idempatant, Like if I click submit
- 5:27Speaker 1: on an online payment form five times because my internet
- 5:31Speaker 1: is lagging. Oh no, and that form uses a post request,
- 5:34Speaker 1: I might accidentally get charged five separate times.
- 5:37Speaker 2: A very painful real world example, but absolutely correct. That
- 5:40Speaker 2: is exactly why developers must choose their verbs carefully.
- 5:44Speaker 1: Yeah, that makes a lot of sense.
- 5:45Speaker 2: And there are other methods too, like the head request,
- 5:47Speaker 2: which basically asks the server to only send the metadata
- 5:50Speaker 2: about a page without downloading all the actual content.
- 5:53Speaker 1: Interesting, what if I want to remove my data? Is
- 5:56Speaker 1: there a command for destroying a resource?
- 5:58Speaker 2: There is the delete method. But and this is a
- 6:01Speaker 2: big but, it's important to note that just because a
- 6:04Speaker 2: method exists in the HTTP protocol doesn't mean a server
- 6:07Speaker 2: will actually honor it.
- 6:08Speaker 1: Well, they're not just gonna let anyone delete things exactly.
- 6:10Speaker 2: A public website will happily accept a get request for
- 6:14Speaker 2: an article, but if your script sends a delete request
- 6:17Speaker 2: to that same article, the server will check your permissions,
- 6:20Speaker 2: see you on an admin, and just reject the request entirely.
- 6:24Speaker 1: Well, that actually leads perfectly into how the server actually responds.
- 6:27Speaker 1: Let's say my script sends a valid get request, the
- 6:30Speaker 1: server hears it, processes it, and talks back. What does
- 6:34Speaker 1: that return package actually look like.
- 6:36Speaker 2: Every HTTP response has two main structural components, the headers
- 6:40Speaker 2: and the body. Okay, the headers are the metadata. They
- 6:43Speaker 2: contain hidden configurations, details about the server software, the date,
- 6:47Speaker 2: security policies. The body, on the other hand, is the
- 6:51Speaker 2: actual payload you requested.
- 6:53Speaker 1: So if I ask for a web page, the body
- 6:55Speaker 1: contains the HTML.
- 6:56Speaker 2: Code exactly, and if you queried an API, the body
- 7:00Speaker 2: might contain JSON data.
- 7:02Speaker 1: Let's define Jason really quickly for anyone unfamiliar because it
- 7:05Speaker 1: pops up constantly in data extraction. It's dense for JavaScript
- 7:09Speaker 1: object notation.
- 7:10Speaker 2: Right, yes it does. And unlike HTML, which is a
- 7:13Speaker 2: markup language full of you know, messy tags designed to
- 7:17Speaker 2: format text for human eyes, right, Jason is a lightweight
- 7:20Speaker 2: data interchange format. It organizes data into clean, readable key
- 7:24Speaker 2: value pairs like dictionaries and lists. It is highly structured,
- 7:28Speaker 2: which makes it incredibly easy for a machine to parse.
- 7:31Speaker 1: That's super helpful. So along with the headers in the body,
- 7:34Speaker 1: the server also sends a status code. I know, the
- 7:36Speaker 1: famous four or four page, which means not found. So
- 7:39Speaker 1: wait when I get at four oh four, that's just
- 7:41Speaker 1: the server calmly using a standardized code to tell me
- 7:43Speaker 1: I messed up and asked for something that doesn't exist.
- 7:46Speaker 2: That's exactly what it is. The status codes are this
- 7:48Speaker 2: universal shorthand for the outcome of your request. The two
- 7:51Speaker 2: hundred level codes, specifically two hundred indicate absolute success. The
- 7:56Speaker 2: server received your request, understood it, and is handing you
- 7:59Speaker 2: the data.
- 8:00Speaker 1: Awesome. And what about the other numbers?
- 8:01Speaker 2: Well, the three hundred level codes indicate redirections. You asked
- 8:05Speaker 2: for a resource, but it moved, so the server is
- 8:07Speaker 2: automatically pointing your client to the new location.
- 8:10Speaker 1: And the four hundred level codes, like our favorite.
- 8:12Speaker 2: Four O four, those are client errors. The server is
- 8:15Speaker 2: stating that the request was malformed or the resource is missing. Basically,
- 8:19Speaker 2: the fault lies with the sender. Conversely, five hundred level
- 8:23Speaker 2: codes indicate server errors.
- 8:24Speaker 1: So my request was perfectly fine, but the server dropped the.
- 8:27Speaker 2: Ball right the server crash, timed out, or encountered some
- 8:30Speaker 2: internal database issue.
- 8:32Speaker 1: Okay, so we've outlined this manual conversation. A client sends
- 8:36Speaker 1: a GT request, the server responds with a two hundred
- 8:38Speaker 1: status code and hands over this massive body of HTML.
- 8:42Speaker 1: But now we hit the core problem. How could a
- 8:45Speaker 1: human possibly read thousands of raw HTML pages a day
- 8:49Speaker 1: to find like specific competitor pricing. They can't, right, we
- 8:53Speaker 1: have to teach a machine to read it for us.
- 8:54Speaker 2: Which brings us to the actual mechanics of web scraping.
- 8:57Speaker 2: Scraping is fundamentally a two stage pipeline. The first stage
- 9:01Speaker 2: is fetching, programmatically executing that HTTP request to download that
- 9:05Speaker 2: giant wall of HTML text to your local.
- 9:08Speaker 1: Machine, okay, And the second stage.
- 9:10Speaker 2: The second stage is parsing, navigating that text to extract
- 9:13Speaker 2: only the highly structured data points you actually care about.
- 9:16Speaker 1: Fetching and parsing, yeah, you know. Fetching is sort of
- 9:19Speaker 1: like go to the library and grabbing a massive encyclopedia,
- 9:23Speaker 1: and then parsing is using the index to instantly flip
- 9:27Speaker 1: to the exact paragraph you need.
- 9:28Speaker 2: That's a perfect way to visualize it. And the parsing
- 9:31Speaker 2: part is where I think people really struggle to visualize
- 9:33Speaker 2: the process.
- 9:34Speaker 1: Well for sure, because when I download an entire web page,
- 9:36Speaker 1: I don't want the navigation men use the footerlengths or
- 9:39Speaker 1: all those sidebar ads. I just want the product title
- 9:41Speaker 1: and the price tag.
- 9:43Speaker 2: But an HTML document isn't just a flat text file.
- 9:46Speaker 1: Is it. No, It's not. HTML's designed with a very
- 9:49Speaker 1: specific nested architecture called the document object model or DOM.
- 9:54Speaker 1: It is a hierarchical tree structure kind of.
- 9:56Speaker 2: Like a family tree.
- 9:57Speaker 1: Think of it exactly like a family tree. The main
- 10:00Speaker 1: HTML document is the great grandparent. Branching off of that,
- 10:04Speaker 1: you have a head section and a body section.
- 10:06Speaker 2: Down the body branch, you might have a div container
- 10:09Speaker 2: representing a product card. Inside that product card, you have
- 10:12Speaker 2: children elements, an image tag, a header tag for the title,
- 10:16Speaker 2: and a paragraph tag containing the price.
- 10:18Speaker 1: So parsing isn't just hitting sutral plus F and searching
- 10:22Speaker 1: for a word. Parsing is basically writing a rule that
- 10:25Speaker 1: tells your script how to climb the tree. Like you
- 10:28Speaker 1: tell it, Go down the body branch, find the specific
- 10:31Speaker 1: product container, go to his children, and grab the text
- 10:34Speaker 1: inside the price tag exactly.
- 10:36Speaker 2: And doing that manually in Python using basic string manipulation
- 10:40Speaker 2: would be an absolute nightmare. I can imagine that is
- 10:42Speaker 2: why developers rely on specialized parsing libraries. The most famous
- 10:47Speaker 2: in the Python ecosystem being Beautiful Soup. Beautiful Soup takes
- 10:50Speaker 2: that chaotic wall of HTML text, builds the family tree
- 10:54Speaker 2: in your computer's memory, and just gives you simple commands
- 10:57Speaker 2: to navigate the branches. Wow, you can literally say find
- 11:00Speaker 2: all divtags with the class name price and it isolates
- 11:03Speaker 2: them instantly.
- 11:04Speaker 1: Okay, so that covers a parsing side. But as you mentioned,
- 11:06Speaker 1: we have to fetch the HTML before we can parse it.
- 11:10Speaker 1: And looking at the landscape of Python HTDP client libraries,
- 11:13Speaker 1: it can feel honestly like a jungle. If I'm building
- 11:16Speaker 1: a scraper, I see libraries like requests, httplip two and eerlib.
- 11:21Speaker 1: They all seemingly do the exact same thing.
- 11:23Speaker 2: Yeah, they really do.
- 11:24Speaker 1: So with so many options, how do you avoid analysis paralysis?
- 11:29Speaker 1: If I'm sitting down to code right now, which one
- 11:31Speaker 1: do I pick?
- 11:32Speaker 2: It is a notorious point of confusion because, like you said,
- 11:35Speaker 2: their functionalities overlap heavily. The key is understanding their different
- 11:39Speaker 2: design philosophies. If you want a high level, incredibly intuitive
- 11:43Speaker 2: interface that handles most of the heavy lifting for you, your
- 11:46Speaker 2: default choice should be the requests library.
- 11:49Speaker 1: Okay, Requests is the default.
- 11:50Speaker 2: Yeah, it makes writing web requests and Python almost as
- 11:53Speaker 2: simple as speaking plain English.
- 11:54Speaker 1: But I'm guessing there are scenarios where you don't want
- 11:57Speaker 1: the tool making all the decisions for you. I read
- 11:59Speaker 1: that hgplib too, which was actually an enhanced version of
- 12:03Speaker 1: an older library built by Google.
- 12:04Speaker 2: Right now, Yes, that's correct.
- 12:06Speaker 1: Right, So I read that it gives you granular control
- 12:08Speaker 1: over the HTTP parameters. But what does granular control actually
- 12:12Speaker 1: mean in a real world context? Why wouldn't I just
- 12:15Speaker 1: use requests for everything.
- 12:17Speaker 2: Let's use redirections as an example. Remember those three hundred
- 12:20Speaker 2: level status codes we talked about.
- 12:21Speaker 1: Yep.
- 12:22Speaker 2: By default, a high level library like requests will automatically
- 12:26Speaker 2: follow a redirect. It wants to get you to the
- 12:27Speaker 2: final destination smoothly. But imagine you are building a cybersecurity
- 12:32Speaker 2: tool to analyze suspicious links. You really don't want your
- 12:35Speaker 2: script blindly following a redirect to a malicious payload.
- 12:39Speaker 1: Ah, I see, you want to intercept the conversation exactly.
- 12:42Speaker 2: With htt flib two you have the fine grained control
- 12:45Speaker 2: to tell the client if you encounter a three hundred
- 12:48Speaker 2: level redirect, do not follow it, Stop and let me
- 12:51Speaker 2: inspect the header to see exactly where this server's trying
- 12:53Speaker 2: to send me. It requires more code to write, but
- 12:56Speaker 2: it offers precision.
- 12:57Speaker 1: That makes total sense. Yeah. Now, both requests and h
- 13:00Speaker 1: ttplib two are third party packages. Meaning I have to
- 13:02Speaker 1: install them separately. But what about earlib. I know that
- 13:05Speaker 1: one comes prepackaged in the Python Standard Library.
- 13:08Speaker 2: Yes, erlib requires no external installation, which makes it ideal
- 13:12Speaker 2: for restricted environments. It's worth noting, though, that if you
- 13:15Speaker 2: look at older tutorials you might see references to both
- 13:19Speaker 2: earlib and arlib two. In the era of Python two,
- 13:22Speaker 2: these were separate libraries with confusingly overlapping functions. It was
- 13:26Speaker 2: a mess for developers.
- 13:28Speaker 1: So how did they fix it? For Python three?
- 13:29Speaker 2: They merged the best elements of both into a single
- 13:32Speaker 2: earlib package and organized it using name spaces.
- 13:35Speaker 1: Okay, name spaces.
- 13:36Speaker 2: Yeah. A name space is essentially a folder system within
- 13:39Speaker 2: the code that keeps tools logically separated so their names
- 13:43Speaker 2: don't clash. Modern arlib has four distinct name spaces. There's
- 13:47Speaker 2: a request for opening URLs error for catching issues like
- 13:50Speaker 2: a four H four pars for breaking down the components
- 13:53Speaker 2: of the URL string itself, and robot parser for reading
- 13:57Speaker 2: a site's rules about what you were actually allowed to scrape.
- 13:59Speaker 1: Oh, it's really structured. And just to round out the toolkit.
- 14:02Speaker 1: If I don't even want to write a pipeon script,
- 14:04Speaker 1: if I just wanted to ping a server directly from
- 14:06Speaker 1: my computer's command line terminal to see if it's a
- 14:08Speaker 1: live I'd use curl right exactly.
- 14:10Speaker 2: Curl is a command line tool that is universally available
- 14:13Speaker 2: across operating systems. It is the quickest way to fire
- 14:16Speaker 2: off an HTTP request and view the raw response directly
- 14:20Speaker 2: in your terminal window.
- 14:22Speaker 1: Okay, let's actually take one of these Python libraries out
- 14:24Speaker 1: for a spin. Let's do a practical mental walkthrough using
- 14:28Speaker 1: htt pillow two. We need a safe testing sandbox, though,
- 14:32Speaker 1: I mean, we shouldn't fire automated requests at a real
- 14:34Speaker 1: business website while we are just learning the ropes.
- 14:37Speaker 2: Definitely not a Standard practice for developers is to use
- 14:40Speaker 2: a service called http bind dot org. It is an
- 14:43Speaker 2: open source endpoint designed specifically for testing HTTP clients. Oh perfect, Yeah.
- 14:48Speaker 2: It accepts get te, post, PEWT, and delete requests, and
- 14:53Speaker 2: it simply mirrors your request back to you so you
- 14:55Speaker 2: can verify your code as working. You can even force
- 14:57Speaker 2: it to return specific status codes like a five hundred
- 15:00Speaker 2: server error to ensure your scraper's air handling logic actually
- 15:04Speaker 2: functions properly.
- 15:05Speaker 1: Okay, let's visualize the code execution step by step. I
- 15:08Speaker 1: have my blank script open. My first line is import
- 15:11Speaker 1: htt plum too.
- 15:12Speaker 2: What happens next, Well, you need to create an instance
- 15:14Speaker 2: of the client. You write a line of code that
- 15:16Speaker 2: assigns the httplub two dot http object to a variable.
- 15:21Speaker 2: You now have a dormant client ready to act.
- 15:24Speaker 1: Next, I need to send that client out to fetch data.
- 15:27Speaker 1: So I invoke the dot request function on that object,
- 15:29Speaker 1: and I feeded the URL do http bend dot org.
- 15:32Speaker 1: I don't specify any extra parameters, so the library just
- 15:35Speaker 1: assumes I want to make a standard gaed request.
- 15:38Speaker 2: Right, and then the script reaches out over the network,
- 15:41Speaker 2: connects to the server, and waits When the server replies
- 15:44Speaker 2: that request function finishes executing and returns two distinct variables
- 15:48Speaker 2: back to your script.
- 15:49Speaker 1: Okay.
- 15:50Speaker 2: The first variable holds the response headers. The second holds
- 15:53Speaker 2: the response body, which is usually assigned to a variable
- 15:55Speaker 2: just named data.
- 15:56Speaker 1: Let's inspect the headers variable first. What is the metadata
- 15:59Speaker 1: actually look like?
- 16:00Speaker 2: It acts like a Python dictionary, a simple list of
- 16:03Speaker 2: key value pairs. You can query the status key and
- 16:06Speaker 2: it will return the integer two hundred. You might see
- 16:09Speaker 2: a server key that identifies the software httpbin dot org
- 16:12Speaker 2: is running. You'll also see a content location key verifying
- 16:16Speaker 2: the final URL.
- 16:17Speaker 1: So my script checks the status it sees a two hundred.
- 16:21Speaker 1: It knows the request was successful. Now I want to
- 16:24Speaker 1: look at the data variable to see the HTML body.
- 16:27Speaker 1: If I print data to my console, do I just
- 16:29Speaker 1: see the readable htmltech?
- 16:31Speaker 2: No? And this is where almost every self tap programmer
- 16:33Speaker 2: gets stuck the first time.
- 16:34Speaker 1: Oh really?
- 16:35Speaker 2: Yeah. When that information travels across the network, it isn't
- 16:38Speaker 2: sent as text. It is returned as raw bytes, essentially
- 16:42Speaker 2: binary data. If you try to feed raw bytes into
- 16:45Speaker 2: a text parser like beautiful Soup, Python will immediately throw
- 16:48Speaker 2: an error because it cannot perform string operations on bitar rays.
- 16:52Speaker 1: Ah. So I have the payload, but it's locked in
- 16:54Speaker 1: a format the parser can't read. How do I translate it?
- 16:57Speaker 2: You have to invoke a built in method called decode
- 17:00Speaker 2: on that data variable. Calling dot d code takes the
- 17:03Speaker 2: raw bite array and translates it into a readable string format,
- 17:06Speaker 2: almost universally UTF eight. Only after you decode the bytes
- 17:10Speaker 2: into a string can you actually begin navigating the htmltree.
- 17:13Speaker 1: Wow, it's a tiny one line step, but if you
- 17:15Speaker 1: don't understand why data travels as bytes versus strings. It
- 17:19Speaker 1: feels like an insurmountable roadblock. I want to try one
- 17:21Speaker 1: more thing though. Htppbend dot org is a great sandbox.
- 17:26Speaker 1: But what if we change the URL in our dot
- 17:28Speaker 1: request function to point at Google dot com.
- 17:31Speaker 2: Oh that's fun. If you point the script at Google,
- 17:34Speaker 2: you are executing the exact same network operation your browser does,
- 17:38Speaker 2: but you're intercepting the raw, unfiltered conversation. When you inspect
- 17:42Speaker 2: the header's dictionary returned by Google, alongside the status code
- 17:45Speaker 2: and server info, you will frequently spot a specific header
- 17:48Speaker 2: labeled setcookie.
- 17:50Speaker 1: This is the part that genuinely blew my mind. When
- 17:53Speaker 1: I write a Python script to quietly hit Google, I
- 17:55Speaker 1: can actually see the exact tracking cookie they use for
- 17:58Speaker 1: ads being handed to me in the raw metadata.
- 18:01Speaker 2: Yes, the sect cookie command is the server explicitly instructing
- 18:04Speaker 2: your client store this unique string of characters. The next
- 18:07Speaker 2: time you send me a GD request, include this string
- 18:10Speaker 2: so I know it's still you. When you use Chrome
- 18:13Speaker 2: or Safari, the browser silently accepts that command, stores the cookie,
- 18:17Speaker 2: and tracks your session without you ever seen the instruction.
- 18:20Speaker 1: But because we build our own client, because we peaked
- 18:23Speaker 1: into the raw HTDP headers, we intercept the command itself.
- 18:27Speaker 1: It really demystifies how tracking actually functions across the Internet.
- 18:31Speaker 1: It's no longer just an abstract concept. It's a visible
- 18:34Speaker 1: line of.
- 18:35Speaker 2: Code, and that is the true value of learning this
- 18:38Speaker 2: programmatic approach. You aren't just memorizing syntax to pull prices
- 18:41Speaker 2: off a web page. You are learning the fundamental architecture
- 18:44Speaker 2: of Internet communication.
- 18:46Speaker 1: We have covered incredible ground today in this deep dive.
- 18:48Speaker 1: We took the split second it takes to load a
- 18:50Speaker 1: web page and expanded it into an entire ecosystem.
- 18:53Speaker 2: It really did.
- 18:54Speaker 1: We explored the difference between simply reading a page with
- 18:57Speaker 1: a GD request and modifying server data with POA put.
- 19:01Speaker 1: We outline the two stage pipeline of webscraping fetching raw
- 19:04Speaker 1: bites using tools like requests or HTP clib two, decoding
- 19:08Speaker 1: those boxx into strings, and then utilizing parsers to climb
- 19:11Speaker 1: the HTML family tree and distract targeted data.
- 19:14Speaker 2: So as you continue exploring these concepts, here's a quick
- 19:17Speaker 2: practical review exercise to anchor your understanding. The next time
- 19:21Speaker 2: you browse a website or fill out a form online
- 19:23Speaker 2: and click submit, pause and ask yourself, did my browser
- 19:27Speaker 2: just execute a ginny T request or a PSD request
- 19:30Speaker 2: based on what I submitted.
- 19:31Speaker 1: Great exercise.
- 19:32Speaker 2: And furthermore, if you were tasked with writing a Python
- 19:35Speaker 2: script to automate that specific interaction, which HTTP library would
- 19:40Speaker 2: provide the right balance of simplicity and control for the job.
- 19:43Speaker 1: Keep that mental checklist running. But before we sign off,
- 19:46Speaker 1: I want to leave you with a lingering challenge, something
- 19:49Speaker 1: really provocative to think about.
- 19:50Speaker 2: Oh, I like where this is going.
- 19:52Speaker 1: Everything we discussed today relies on scraping static HTML pages
- 19:57Speaker 1: where the server hands you all the information in one
- 19:59Speaker 1: CLI parsible document. But the Internet is shifting rapidly. Websites
- 20:04Speaker 1: are becoming overwhelmingly dynamic, relying on heavily obviouscated JavaScript to
- 20:08Speaker 1: pull data in long after the initial HTML is delivered.
- 20:12Speaker 2: Yeah, the traditional fetch and parse method really begins to
- 20:15Speaker 2: break down when that initial payload is basically empty. The
- 20:18Speaker 2: tools simply must become more sophisticated.
- 20:21Speaker 1: Exactly when these basic static fetching scripts can no longer
- 20:24Speaker 1: read the modern web, how will the invisible conversation have
- 20:27Speaker 1: to evolve? And what happens to our automated systems when
- 20:30Speaker 1: servers stop handing over pre made pages and only provide abstract,
- 20:34Speaker 1: dynamic code. Think about how you would solve that puzzle.
- 20:37Speaker 1: Until next time, Keep diving deep