Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 41: Mastering GET and POST Form Submissions
Transcript
- 0:00Speaker 1: I want you to picture something for a second. You're
- 0:02Speaker 1: sitting at your computer and you're doing something you do
- 0:04Speaker 1: probably a dozen times a day. You type a search
- 0:07Speaker 1: term into a massive e commerce site like Amazon or Walmart,
- 0:11Speaker 1: right right, yeah, and you hit enter and then boom,
- 0:14Speaker 1: magic happens. A customized list of products just pops onto
- 0:18Speaker 1: your screen. But what is actually happening behind the scenes
- 0:21Speaker 1: when we submit that data to a server, Like, how
- 0:24Speaker 1: does a process our intent?
- 0:26Speaker 2: Well, it's funny you call it magic, because you know,
- 0:29Speaker 2: when you're first learning about programming or data science, it
- 0:31Speaker 2: really does feel that way totally. But it's not magic.
- 0:34Speaker 2: It's essentially a digital contract. Basic static websites, they don't
- 0:38Speaker 2: really require you to know much. Pulling text off a
- 0:40Speaker 2: flat article is one thing, But interacting with search bars
- 0:43Speaker 2: and massive databases that is essential for any aspiring tech.
- 0:47Speaker 1: Enthusiast exactly, which is the whole mission of our Deep
- 0:50Speaker 1: Die today. We are going to learn how to master
- 0:53Speaker 1: web scraping by understanding how to programmatically handle these web
- 0:57Speaker 1: form submissions.
- 0:59Speaker 2: Yeah, because to get that dynamic data, you can't just
- 1:01Speaker 2: passively read a page. You have to actively converse with
- 1:04Speaker 2: the server. And to do that you need to know
- 1:08Speaker 2: the map, the map. I like that, right, So we're
- 1:10Speaker 2: going to break down the two dominant types of web
- 1:12Speaker 2: forms you'll encounter out in the wild. That's get forms
- 1:16Speaker 2: and post forms, and you know how to actually write
- 1:20Speaker 2: Python code to handle them.
- 1:21Speaker 1: Okay, let's unpack this. We'll start with the first type,
- 1:23Speaker 1: which I think you said is the most common, right,
- 1:25Speaker 1: the get form.
- 1:27Speaker 2: Yeah, exactly. A geet form is basically how the vast
- 1:30Speaker 2: majority of simple web pages work, and it's defining characteristic
- 1:35Speaker 2: is transparency.
- 1:37Speaker 1: Transparency. What do you mean by that?
- 1:39Speaker 2: Well, the parameters of what you're searching for are placed
- 1:41Speaker 2: directly out in the open, right in the URL. So
- 1:44Speaker 2: if you go to a huge database like the Internet
- 1:46Speaker 2: movie database IMDb, and you search for an exact name
- 1:50Speaker 2: match let's say, an actor name mark.
- 1:52Speaker 1: Right, I just type mark into the search bar.
- 1:55Speaker 2: You do, and when you hit enter, you arrive at
- 1:57Speaker 2: a new page. But if you look up at your
- 2:00Speaker 2: browser's address bar, the URL has completely changed.
- 2:03Speaker 1: Right, It's not just MDB dot dot com anymore, exactly.
- 2:06Speaker 2: It's a long string of text and right at the
- 2:09Speaker 2: end you'll see a question mark followed by something like
- 2:13Speaker 2: qequals mark.
- 2:14Speaker 1: Oh, Wow. I see that all the time and I
- 2:16Speaker 1: just sort of ignore it. But it's like a postcard.
- 2:18Speaker 2: A postcard, Yeah, think about it.
- 2:20Speaker 1: A get form is literally like a postcard. Anyone handling
- 2:24Speaker 1: it can read the message right on the back. The
- 2:26Speaker 1: que in that URL literally denotes the query, the term
- 2:31Speaker 1: you searched for. It's just sitting there in plaintext.
- 2:33Speaker 2: That's a perfect analogy. Actually, the question mark is the
- 2:37Speaker 2: dividing line between the server address and your specific instructions.
- 2:42Speaker 2: And because it's a postcard, anyone, your local router, the
- 2:45Speaker 2: internet service provider can read it without opening an envelope.
- 2:48Speaker 1: So if I want to scrape this data using Python,
- 2:50Speaker 1: I basically just have to forge that postcard. Right.
- 2:53Speaker 2: You hit the nail on the head. You don't have
- 2:54Speaker 2: to trick a browser into clicking a button. You just
- 2:57Speaker 2: generate the right URL.
- 2:59Speaker 1: Walk me through the actual code for that, like step
- 3:01Speaker 1: by step.
- 3:02Speaker 2: Okay, so first you import your necessary packages for this.
- 3:05Speaker 2: You really just need two foundational Python libraries, Requests and
- 3:09Speaker 2: beautiful Soup.
- 3:10Speaker 1: Okay, requests and beautiful Soup.
- 3:12Speaker 2: Got it. So you use the get mechod from the
- 3:15Speaker 2: request library. You literally type request stut get and you
- 3:18Speaker 2: feed it that full URL string with your q equals
- 3:22Speaker 2: mark parameter.
- 3:23Speaker 1: And Python basically acts as my browser firing off that
- 3:26Speaker 1: postcard exactly.
- 3:27Speaker 2: It retrieves the raw HTML data from the URL. But
- 3:30Speaker 2: you know, eahml is just a massive wall of text
- 3:33Speaker 2: and angled brackets. It's really messy, right.
- 3:35Speaker 1: So how do I pull just the actors names.
- 3:37Speaker 2: Out of that mess That's where you pass the content
- 3:40Speaker 2: through the built in parser in beautiful Soup. You don't
- 3:43Speaker 2: just search the text. You go to the actual web
- 3:45Speaker 2: page in your regular browser first write click and hit
- 3:49Speaker 2: inspect ah.
- 3:50Speaker 1: The inspect element tool. I love that thing.
- 3:52Speaker 2: It's the best. You use it to find these specific
- 3:55Speaker 2: HTML tags or CSS classes that hold the actors' names.
- 3:59Speaker 2: Then you tell beautiful Soup to find those exact elements
- 4:02Speaker 2: in the raw data it downloaded, and it just.
- 4:04Speaker 1: Plucks them right out. That is incredibly powerful, And because
- 4:07Speaker 1: all the data is right there in the URL, gettt
- 4:10Speaker 1: forms are incredibly easy to share too, Like I can
- 4:12Speaker 1: copy and paste that link to a friend and they'll
- 4:14Speaker 1: see the exact same list of actors.
- 4:16Speaker 2: Yes, because the URL contains the entire state of the search.
- 4:21Speaker 2: It's brilliant.
- 4:22Speaker 1: But wait a minute, if geet forms are so easy
- 4:24Speaker 1: and so shareable, why do we need anything else? Like
- 4:27Speaker 1: what happens when a website doesn't want to put all
- 4:29Speaker 1: its cards on the table in the URL?
- 4:31Speaker 2: Well, think about your postcard analogy. What if you're sending
- 4:34Speaker 2: your bank account number or a password?
- 4:37Speaker 1: Oh right, I definitely don't want that written on the
- 4:39Speaker 1: back of a postcard for my WiFi router to log.
- 4:42Speaker 2: Exactly when you're dealing with sensitive data or when a
- 4:45Speaker 2: request actually changes something on the server, like updating a profile,
- 4:48Speaker 2: you need a sealed envelope. And that introduces us to
- 4:52Speaker 2: the hidden reality of PST.
- 4:54Speaker 1: Forms the post form okay.
- 4:56Speaker 2: And I have to warn you these are fundamentally not
- 4:59Speaker 2: scrape friendly. Unlike geet forms, PSTR forms do not change
- 5:03Speaker 2: the URL to reflect the search parameters, so the data
- 5:06Speaker 2: is just hidden completely. Let me give you a real
- 5:09Speaker 2: world example. There's this database run by the California Department
- 5:12Speaker 2: of Consumer Affairs the DCA. They license practitioners across like
- 5:17Speaker 2: two hundred and fifty five different professions.
- 5:19Speaker 1: Okay, So let's say I want to find a specific
- 5:21Speaker 1: dentist in California.
- 5:22Speaker 2: Right, you go to the site. It's like the Dental
- 5:25Speaker 2: Board of California. You're from a dropdown. You choose dentist license,
- 5:28Speaker 2: and let's say you leave the license number blank, but
- 5:31Speaker 2: you type in the first name John and the last.
- 5:34Speaker 1: Name Kelly standard search form staff standard stuff.
- 5:36Speaker 2: You hit search, the database churns, and it spits back
- 5:40Speaker 2: a result showing say, eight people holding that specific license.
- 5:43Speaker 1: Here's where it gets really interesting, though. If you check
- 5:46Speaker 1: the URL on this results page, it gives absolutely no indication.
- 5:51Speaker 2: Of your search none at all.
- 5:52Speaker 1: It's just a generic web address. There's no query string,
- 5:55Speaker 1: no question mark, no John Kelly anywhere. The parameters are
- 5:59Speaker 1: entirely hidden.
- 6:00Speaker 2: Right. Building on what we said earlier, a POSTD form
- 6:03Speaker 2: is like a sealed envelope. You securely hand the data
- 6:06Speaker 2: to the server, but it's completely hidden from the outside observer.
- 6:09Speaker 1: And it's hidden from a simple URL scrape too.
- 6:12Speaker 2: Exactly. If you open Python and try that request dot
- 6:16Speaker 2: get trick on the generic URL of the results page,
- 6:19Speaker 2: you will.
- 6:19Speaker 1: Get nothing back, really just an error.
- 6:21Speaker 2: Basically, you can even check the response text in Python
- 6:25Speaker 2: and it'll confirm it's empty or just a blank form
- 6:28Speaker 2: because the server is looking at your request saying where's
- 6:31Speaker 2: the envelope?
- 6:31Speaker 1: That makes total sense. So to succeed, my Python code
- 6:35Speaker 1: essentially has to pretend to be a human being physically
- 6:37Speaker 1: filling out the drop downs and clicking the button exactly.
- 6:40Speaker 2: But how does Python know what to put in the
- 6:42Speaker 2: envelope if we can't see the URL changing?
- 6:45Speaker 1: Yeah, that's the million dollar question. How do we crack
- 6:49Speaker 1: the post form code?
- 6:51Speaker 2: What's fascinating here is we actually have to spy on ourselves.
- 6:55Speaker 1: Spy on ourselves, I love it. Tell me more.
- 6:58Speaker 2: We use a clever workaround leveraging Chrome's developer tools, specifically
- 7:03Speaker 2: the network tab. Who basically set up a wire tab
- 7:05Speaker 2: on our own browser to see the hidden request right
- 7:08Speaker 2: as it's being said.
- 7:09Speaker 1: Okay, so walk me through the step by step for
- 7:11Speaker 1: doing this.
- 7:11Speaker 2: Sure, you go to that blank DCA search form before
- 7:14Speaker 2: you touch anything. You go to the view tab in
- 7:16Speaker 2: your browser, down to developer and click on developer tools.
- 7:20Speaker 2: Then you click over to the network tab. It usually
- 7:22Speaker 2: just looks like a blank gray screen at first.
- 7:24Speaker 1: Just waiting for something to happen.
- 7:25Speaker 2: Exactly, leave it open, go back to the web page,
- 7:28Speaker 2: fill out the form just like we did John Kelly Dentist,
- 7:31Speaker 2: and hit submit. The moment you do, that network tab
- 7:35Speaker 2: is going to bustle with activity.
- 7:36Speaker 1: Because it's logging every single piece of data flying back
- 7:39Speaker 1: and forth.
- 7:40Speaker 2: Right, you'll see dozens of items load. You want to
- 7:43Speaker 2: scroll to the very top of that pain. So, like
- 7:46Speaker 2: the main document, request for the URL you're at, and
- 7:48Speaker 2: then look over in the section called headers.
- 7:50Speaker 1: The headers.
- 7:51Speaker 2: Okay, if you scroll down inside the headers, you'll eventually
- 7:54Speaker 2: find a section labeled form data or sometimes payload, and
- 7:58Speaker 2: that's the jack cut is the jackpot. That section literally
- 8:02Speaker 2: reveals the internal blueprint of the sealed envelope. It shows
- 8:06Speaker 2: you the exact variable names the database expects. So instead
- 8:09Speaker 2: of guessing, you see that the server wants like board
- 8:12Speaker 2: code equals twenty and first name equals John.
- 8:15Speaker 1: So now I just take that browsersleuthing and translate it
- 8:17Speaker 1: into Python. Yep, I take all that form data I
- 8:20Speaker 1: just found, and I convert it into a standard Python dictionary.
- 8:23Speaker 1: Just key value pairs wrapped in curly braces.
- 8:26Speaker 2: You've got it. It's just a slight modification to the
- 8:28Speaker 2: script we wrote earlier.
- 8:30Speaker 1: Right, So instead of request dot get, I'm guessing, I
- 8:32Speaker 1: use requests dot post Exactly.
- 8:35Speaker 2: You call request dot post. You pass in the target
- 8:38Speaker 2: url of the form but crucially, you also pass in
- 8:41Speaker 2: your new dictionary using the data parameter.
- 8:43Speaker 1: Oh so I'm literally handing the requests library, the destination address,
- 8:47Speaker 1: and the fully loaded envelope.
- 8:49Speaker 2: Yes, you say data your dictionary, and when you run that,
- 8:52Speaker 2: the server is tricked. It thinks you interacted with the
- 8:55Speaker 2: actual web page. It opens the envelope process of the data,
- 8:58Speaker 2: and your Python scripts zestfully prints out the names of
- 9:01Speaker 2: the eight dentists. Boom.
- 9:03Speaker 1: That is incredibly satisfying.
- 9:04Speaker 2: It's a huge breakthrough when you first get it to work.
- 9:07Speaker 1: So what does this all mean? Like, by mastering these
- 9:10Speaker 1: two techniques extracting query strings from the URLs for debt
- 9:13Speaker 1: t forms and spying on the network tab to send
- 9:16Speaker 1: data dictionaries for post forms, you are essentially empowered to
- 9:20Speaker 1: scrape the vast majority of websites on the Internet.
- 9:23Speaker 2: Absolutely, you're no longer just looking at a static page.
- 9:25Speaker 2: You're actually talking to the database. But you know, we
- 9:28Speaker 2: should probably mention that servers do fight back.
- 9:31Speaker 1: Oh, they definitely do. It's a whole cat and mouse game, right.
- 9:33Speaker 2: Like they use things called CSRF tokens cross site request
- 9:37Speaker 2: forgery tokens, basically a hidden randomized fingerprint the server puts
- 9:42Speaker 2: on the form when a real human loads it.
- 9:44Speaker 1: So if my Python script just blasts the post request
- 9:48Speaker 1: without visiting the page first.
- 9:49Speaker 2: It gets rejected because it's missing the fingerprint. You'd have
- 9:53Speaker 2: to write a script that does a get tea request
- 9:56Speaker 2: first to steal the token, inject it into your dictionary,
- 9:59Speaker 2: and then send the po request.
- 10:00Speaker 1: Wow, so it's a two step dance.
- 10:02Speaker 2: It really is an arms race. Yeah, but that's the
- 10:04Speaker 2: big picture here. Understanding how data moves between a client
- 10:08Speaker 2: and a server, whether it's transparent or hidden. That's the
- 10:11Speaker 2: absolute foundation of both webscraping and cybersecurity.
- 10:15Speaker 1: It totally demystifies the web. You start looking at every
- 10:17Speaker 1: search bar differently, which actually brings me to a quick
- 10:20Speaker 1: review exercise for you as you're listening today.
- 10:22Speaker 2: Oh this is a great idea.
- 10:23Speaker 1: Yeah, next time you are casually browsing a website, maybe
- 10:27Speaker 1: shopping or using a dropdown menu, I want you to
- 10:29Speaker 1: keep an eye on the URL. Does it change to
- 10:32Speaker 1: show your search terms or does it stay exactly the same.
- 10:35Speaker 2: Try to spot if it's a get tea or.
- 10:37Speaker 1: A post exactly, and if it stays the same, try
- 10:40Speaker 1: opening your browsers developer tools head over to the network tab,
- 10:43Speaker 1: or run the search again and see if you can
- 10:45Speaker 1: spot the hidden form data being sent inside the payload.
- 10:48Speaker 1: It's so cool once you see it with your own eyes.
- 10:50Speaker 2: It really builds that critical technical intuition.
- 10:53Speaker 1: It does, but it also leads me with this lingering
- 10:56Speaker 1: question to ponder, oh, what's that Well, if we can
- 11:00Speaker 1: easily write Python scripts that perfectly mimic human behavior like
- 11:05Speaker 1: filling out these hidden povis T forms and tricking servers
- 11:08Speaker 1: into handing over their data, how can website creators truly
- 11:12Speaker 1: tell the difference between a curious learner and an invisible
- 11:16Speaker 1: army of automated bots.
- 11:18Speaker 2: That is the billion dollar question in tech right now, right, and.
- 11:21Speaker 1: What does that mean for the future of navigating the web?
- 11:23Speaker 1: If the line between user and script keeps getting thinner,
- 11:27Speaker 1: we'll think about the invisible logic guarding the gates the
- 11:30Speaker 1: next time you hit search