Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 2: From HTTP Basics to URL Hacking
Transcript
- 0:00Speaker 1: Imagine every time you buy a plane ticket, or you know,
- 0:03Speaker 1: check a stock price, or even just look for a
- 0:05Speaker 1: used car online, you're competing against an invisible army.
- 0:10Speaker 2: Yeah, and it's a massive army, right.
- 0:12Speaker 1: It's just machines that are capable of reading like a
- 0:15Speaker 1: million web pages before you can even move your mouse.
- 0:17Speaker 1: They never sleep, they don't get distracted by banner ads,
- 0:20Speaker 1: and they just extract data perfectly.
- 0:22Speaker 2: And that invisible army really dictates a huge portion of
- 0:25Speaker 2: modern Internet traffic. I mean, the financial sector relies on
- 0:28Speaker 2: it for real time market data. E commerce platforms use
- 0:32Speaker 2: it to undercut competitors, oh constantly exactly, and researchers use
- 0:36Speaker 2: it to build massive data sets. Really understanding how to
- 0:39Speaker 2: command this kind of automated browsings. It gives you a
- 0:43Speaker 2: serious superpower for research totally, and.
- 0:45Speaker 1: That is exactly what we're building today. So welcome to
- 0:49Speaker 1: today's deep dive. Our mission is to really pull back
- 0:52Speaker 1: the curtain on how machines browse the Internet.
- 0:54Speaker 2: We're basically bypassing the visual web entirely, right.
- 0:58Speaker 1: We're going to explore the mechanics of webscrape, figure out
- 1:01Speaker 1: the hidden language of HTTP, and learn this clever trick
- 1:04Speaker 1: called url hagging.
- 1:06Speaker 2: Yeah, to set the stage here, the materials we're looking
- 1:08Speaker 2: at today make a really important distinction. Humans browse the
- 1:12Speaker 2: web emotionally. We look at pretty pictures, we care about layout.
- 1:17Speaker 2: Computers just browse it purely for data.
- 1:19Speaker 1: Right, So before we build our digital clone, we need
- 1:22Speaker 1: to understand exactly how its behavior is different from our
- 1:25Speaker 1: own everyday surfing. Like, let's break down a normal human
- 1:29Speaker 1: browsing session. Sure, you type in a URL. The browser
- 1:32Speaker 1: downloads the HTML, but then it has to parse it,
- 1:35Speaker 1: pull in images, render this beautiful page.
- 1:38Speaker 2: It fetches fonts, executes JavaScript for animations.
- 1:41Speaker 1: Yeah, it acts like this really hard working theatrical crew
- 1:44Speaker 1: desperately building a stage set just so the human audience
- 1:47Speaker 1: has something nice to look.
- 1:48Speaker 2: At, exactly. But if we're building a scraper, we don't
- 1:50Speaker 2: care about the stage set at.
- 1:52Speaker 1: All, right, we just want the script.
- 1:53Speaker 2: Yeah, the scraper starts with what we call a start
- 1:57Speaker 2: URL variable, and it downloads that exact same. But here's
- 2:01Speaker 2: the critical difference. It doesn't render anything. It just halts.
- 2:05Speaker 1: It just stops.
- 2:06Speaker 2: It halts the process immediately. It parses the HTML strictly
- 2:10Speaker 2: into data. What's fascinating here is that the ultimate objective
- 2:14Speaker 2: for both humans and scrapers is the same, which is
- 2:17Speaker 2: driving meaning and insights, right, but the scraper transforms that
- 2:22Speaker 2: data for analysis, and crucially, web scrapers never forget. They
- 2:26Speaker 2: just store it perfectly and move to the next job
- 2:29Speaker 2: at lightning speed.
- 2:30Speaker 1: Okay, let's unpack this because so a webscraper is basically
- 2:34Speaker 1: a speed reader with a photographic memory but zero appreciation
- 2:39Speaker 1: for esthetics.
- 2:40Speaker 2: That's a great way to put it.
- 2:41Speaker 1: It's like it walks into an art gallery completely ignores
- 2:44Speaker 1: the beautiful paintings and just memorizes the dimensions of all
- 2:47Speaker 1: the frames, yes.
- 2:48Speaker 2: Or the chemical composition of the paint It just doesn't
- 2:50Speaker 2: care how the layout makes a user feel. It just
- 2:53Speaker 2: wants the raw data.
- 2:54Speaker 1: Okay, so if scrapers are these hyper efficient speed readers,
- 2:58Speaker 1: how do they actually ask the web servers for the books?
- 3:01Speaker 2: Like?
- 3:02Speaker 1: They have to speak the right language.
- 3:03Speaker 2: Right, which brings us to HTTP.
- 3:06Speaker 1: The hypertext transfer protocol.
- 3:07Speaker 2: Exactly and HTTPS, which is the exact same thing, but
- 3:11Speaker 2: encrypted for security. The entire web runs on just two
- 3:15Speaker 2: types of messages, request messages, which are from you to
- 3:19Speaker 2: the server.
- 3:19Speaker 1: And response messages from the server back to you. You
- 3:23Speaker 1: got it, And I think it's wild that even a
- 3:25Speaker 1: single web page load might trigger like dozens of these
- 3:31Speaker 1: requests and response cycles. Yeah, you know, for every single
- 3:35Speaker 1: image in script.
- 3:36Speaker 2: Oh easily dozens, sometimes hundreds. And what's actually inside those
- 3:40Speaker 2: request messages is pretty simple. It's just a URL and
- 3:43Speaker 2: a verb or a method, a verb like an action word.
- 3:46Speaker 2: Yeah for scraping. There are really two important ones. The
- 3:49Speaker 2: default is giet. It literally just says, get some data
- 3:52Speaker 2: from the server.
- 3:52Speaker 1: Just hand me the file exactly.
- 3:54Speaker 2: But then there's p posts. You use pe posts to
- 3:56Speaker 2: send data to the server.
- 3:57Speaker 1: Oh, like if I'm submitting a log inform to get
- 3:59Speaker 1: pass the paywall or something.
- 4:01Speaker 2: Right, you're trying to persuade a server to give you
- 4:02Speaker 2: restricted data, so you peep host your credentials.
- 4:04Speaker 1: Got it. But wait, if my DT or a POOS
- 4:07Speaker 1: request is perfect, Servers aren't stupid. They look at who's asking.
- 4:10Speaker 2: Yes, they do. And that's where the user agent string
- 4:13Speaker 2: comes in, right, the.
- 4:14Speaker 1: User agent It looks like complete gibberish if you just read.
- 4:16Speaker 2: It, it kind of does, but servers love it. It tells
- 4:19Speaker 2: them your exact browser version, your operating system. It's basically
- 4:23Speaker 2: a fingerprint.
- 4:24Speaker 1: Like showing a passport at the door precisely.
- 4:26Speaker 2: And when you browse normally, your browser handles all of
- 4:29Speaker 2: this automatically. But when you build a scraper, you have
- 4:33Speaker 2: to manage those HTTP details.
- 4:35Speaker 1: Yourself, otherwise the server just blocks you.
- 4:37Speaker 2: Yeah, if your scraper shows up and its passport says
- 4:41Speaker 2: I am a Python automated bot, the server is going
- 4:43Speaker 2: to reject it immediately. You have to forge that passport.
- 4:46Speaker 1: Okay, so we know how our automated browser communicates with HTTP,
- 4:51Speaker 1: and we know how to fake our passport. Now we
- 4:53Speaker 1: need to know exactly where to point it.
- 4:54Speaker 2: We have to dissect the map itself, the URL.
- 4:57Speaker 1: Here's where it gets really interesting, because the URL is
- 5:00Speaker 1: broken into parts. Yeah, if you got the scheme like
- 5:03Speaker 1: HTTP or HGTPS.
- 5:05Speaker 2: Then the host like icars dot dot com.
- 5:07Speaker 1: Right, and the port which is usually eighty for HGPR
- 5:11Speaker 1: or four forty three for h GTPs, and then the path,
- 5:14Speaker 1: which is where the data actually lives like slash used cars.
- 5:17Speaker 2: But for scraping, the most vital part of the URL
- 5:20Speaker 2: is the query string or the URL fragment.
- 5:23Speaker 1: That's the chaotic jumble of text at the very end.
- 5:26Speaker 2: Right, Yeah, it always happens after a question mark or
- 5:28Speaker 2: a hash symbol, and it's essentially just key value pairs
- 5:31Speaker 2: separated by ampersams.
- 5:33Speaker 1: Okay, let's use an example to make this real. Say
- 5:35Speaker 1: you're on a car website and you're searching in Lebanon, Kansas.
- 5:39Speaker 2: Which is the geographical center of America by the way.
- 5:42Speaker 1: Fun fact, zip code sixty six nine five to two.
- 5:45Speaker 1: So you hit search on the website and the query
- 5:47Speaker 1: string up in your address bar might say something like
- 5:50Speaker 1: question mark location equals six to six nine five to
- 5:53Speaker 1: two ampersand radius equals all exactly.
- 5:56Speaker 2: Location is the key sixty six nine five to two
- 5:58Speaker 2: is the value the empersand separates it from the next key,
- 6:01Speaker 2: which is radius.
- 6:02Speaker 1: And the sources for today call this URL hacking. But wait,
- 6:06Speaker 1: hacking sounds illegal. Are we teaching the listener how to
- 6:09Speaker 1: break into a mainframe here?
- 6:11Speaker 2: No? No, not at all. URL hacking just means manipulating
- 6:13Speaker 2: the web address directly instead of using the website's graphical interface.
- 6:17Speaker 1: Oh, because the form's just doing it for you.
- 6:18Speaker 2: Exactly. When you use a search filter form on a site,
- 6:22Speaker 2: the form is literally just changing that query string and
- 6:25Speaker 2: reloading the URL. That's all it does.
- 6:27Speaker 1: So you can bypass the form entirely. Just type a
- 6:30Speaker 1: new zip code right into location equals part of the
- 6:33Speaker 1: address bar.
- 6:33Speaker 2: You're just talking directly to the database. It makes the
- 6:36Speaker 2: graphical form obsolete.
- 6:37Speaker 1: Okay, so we know the language, we know how to
- 6:40Speaker 1: hack the map, how do we actually write the code
- 6:42Speaker 1: to make our scraper? Do they? Heavy lifting?
- 6:45Speaker 2: Python is the ultimate tool for this. Specifically, there's a
- 6:48Speaker 2: feature from Python three point six onward called f strings
- 6:51Speaker 2: formatted string literals.
- 6:54Speaker 1: F strings are amazing. They're so simple. You just put
- 6:56Speaker 1: the letter F right in front.
- 6:57Speaker 2: Of a string of text right and once you do that,
- 6:59Speaker 2: you can draw any variable directly into the text using
- 7:02Speaker 2: curly braces.
- 7:03Speaker 1: So instead of typing out forty thousand URLs by hand,
- 7:06Speaker 1: you just put the zip code variable in curly braces.
- 7:09Speaker 2: And write a loop exactly. It makes it incredibly easy
- 7:12Speaker 2: to loop through thousands of combinations automatically. It generates the.
- 7:15Speaker 1: Map for you, but actually go to those locations. You
- 7:19Speaker 1: need a way to execute that get.
- 7:21Speaker 2: Verb, which brings us to the Python Requests library. It's
- 7:24Speaker 2: actually advertised as HTTP for humans.
- 7:27Speaker 1: I love that because manually writing network sockets sounds awful.
- 7:31Speaker 2: It is with the request library to execute the get verb.
- 7:36Speaker 2: You literally just write requests dot get and you feed
- 7:40Speaker 2: it your start URL.
- 7:42Speaker 1: So what does this all mean? Like, that single line
- 7:44Speaker 1: of code goes out, talks to the server and captures
- 7:47Speaker 1: the entire response.
- 7:49Speaker 2: Yep, it captures the response and saves it as an object.
- 7:52Speaker 2: Say you call it downloaded underscore page.
- 7:54Speaker 1: And if you want to see the actual raw htmlse
- 7:57Speaker 1: you can scrape it. You just append dot text.
- 7:59Speaker 2: To that object right downloadedpage dot text and boom, you've
- 8:03Speaker 2: automated the web.
- 8:04Speaker 1: You've completely bypassed the human browsing experience. You just commanded
- 8:07Speaker 1: a machine to extract raw structural data and load it
- 8:11Speaker 1: into your own database in milliseconds.
- 8:13Speaker 2: It really shifts how you see the Internet. You start
- 8:15Speaker 2: seeing websites not as visual pages, but just as temporary
- 8:18Speaker 2: wrappers for underlying databases.
- 8:19Speaker 1: It really does. And to help cement that, I have
- 8:21Speaker 1: a quick review exercise for you, the listener.
- 8:24Speaker 2: It's a fun one.
- 8:25Speaker 1: Next time you use a search filter on a shopping website,
- 8:27Speaker 1: say you're looking for shoes or whatever. Don't look down
- 8:30Speaker 1: at the page, look up at the URL. Can you
- 8:34Speaker 1: spot the query string?
- 8:35Speaker 2: Yeah, look for the question mark.
- 8:36Speaker 1: Find the key value pairs separated by those ampersands, and
- 8:40Speaker 1: try changing a value right there in the address.
- 8:42Speaker 2: Bar, hit intern and see what the server sends back.
- 8:44Speaker 1: It's empowering when you realize you can just talk directly
- 8:47Speaker 1: to it.
- 8:48Speaker 2: It is, but it also leads to a pretty wild
- 8:51Speaker 2: thought about the future. Oh yeah, think about it. If
- 8:54Speaker 2: scrapers never forget and our automated systems can pull in
- 8:58Speaker 2: data perfectly by by passing the visual interface entirely, what
- 9:03Speaker 2: happens to the web in the future.
- 9:05Speaker 1: That's a great question.
- 9:06Speaker 2: If AI replaces human browsing, will websites eventually just stop
- 9:10Speaker 2: designing pretty pictures altogether? Will they just serve raw data
- 9:13Speaker 2: directly to machines?
- 9:15Speaker 1: Oh wow, so like a web with no art gallery
- 9:17Speaker 1: left to wander through, just data. That is. Man, that's
- 9:21Speaker 1: a heavy thought, but it's an incredibly compelling place to
- 9:24Speaker 1: wrap up. Thank you for joining us on this exploration
- 9:27Speaker 1: of the invisible Internet. Keep experimenting with those query strings
- 9:30Speaker 1: and we'll catch you on the next deep dive.