Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 43: Mastering File Uploads and Reverse Image Search
Transcript
- 0:00Speaker 1: So if you tell your web automation script to seeply
- 0:03Speaker 1: execute a standard click method on a file upload button,
- 0:06Speaker 1: your entire program will well, it'll instantly freeze.
- 0:09Speaker 2: Yeah, it just hanged. I mean the console stalls out
- 0:11Speaker 2: and the automation basically dies.
- 0:13Speaker 1: Right there, which is incredibly frustrating. So today we are
- 0:16Speaker 1: pulling back the curtain to examine exactly why that happens.
- 0:20Speaker 1: We're looking at the rigid boundary between a web browser
- 0:22Speaker 1: and an operating system. Why crossing that boundary breaks bots?
- 0:28Speaker 1: And while the developer cheat code used to bypass it entirely,
- 0:32Speaker 1: So welcome to the deep dive.
- 0:33Speaker 2: Thanks. Yeah, it's a mechanic that fundamentally changes how you
- 0:37Speaker 2: look at the interaction between local environments and web applications.
- 0:40Speaker 1: To be honest, right, because usually when people talk about webscraping,
- 0:43Speaker 1: the focus is just passive observation.
- 0:46Speaker 2: Exactly, sending get requests, parsing the dom tree, maybe extracting
- 0:49Speaker 2: some text. But today we're focusing on active state manipulation.
- 0:54Speaker 1: Which is a whole different ballgame.
- 0:55Speaker 2: It really is. We aren't just reading the website anymore.
- 0:58Speaker 2: We are injecting local data into it, forcing a server
- 1:01Speaker 2: side reaction. And then capturing the output.
- 1:04Speaker 1: Okay, but to really understand this we have to look
- 1:06Speaker 1: at the deployment strategy first, Like, why build a bot
- 1:10Speaker 1: that uploads files in the first place.
- 1:12Speaker 2: Right, the real world application?
- 1:14Speaker 1: Yeah, I mean, if the end goal is just data extraction,
- 1:17Speaker 1: injecting a local file introduces a massive point of friction,
- 1:21Speaker 1: doesn't it.
- 1:22Speaker 2: It does. But looking at the architecture, the need to
- 1:26Speaker 2: upload files during automation essentially serves two distinct strategic purposes.
- 1:31Speaker 1: Okay, what's the first one?
- 1:32Speaker 2: So the first is generating new content. It makes sense
- 1:35Speaker 2: if we're uploading a data set to like generate a
- 1:38Speaker 2: custom report, we have to feed the machine to actually
- 1:42Speaker 2: get the output we want to scrape.
- 1:43Speaker 1: Oh right, like a specialized scientific portal or something exactly.
- 1:47Speaker 2: You pass it a raw CSV file, the back end
- 1:50Speaker 2: crunches the numbers, and then the server renders a highly
- 1:53Speaker 2: detailed dynamic DOM containing your custom results.
- 1:56Speaker 1: Gotcha. So the scrape is the ultimate goal, but the
- 1:59Speaker 1: upload is the new necessary catalyst to get there.
- 2:01Speaker 2: Yeah, beautifully put. But it's not always about content generation.
- 2:05Speaker 2: The second purpose is purely about access, proceeding deeper into.
- 2:09Speaker 1: A site bypassing gates.
- 2:11Speaker 2: Right, Sometimes you literally cannot move forward on a website
- 2:15Speaker 2: without uploading a file.
- 2:16Speaker 1: I mean like submitting a resume to a heavily gated
- 2:19Speaker 1: job application system maybe.
- 2:21Speaker 2: Yes, or pushing a mandatory display picture into a registration
- 2:24Speaker 2: flow like on a dating site. If your script doesn't
- 2:28Speaker 2: know how to negotiate that requirement, it just gets locked out.
- 2:32Speaker 1: So it's basically like interacting with a building. That's second category,
- 2:36Speaker 1: you know, the resumes and dating picks. That's like showing
- 2:38Speaker 1: an ID to a bouncer just to get inside the club.
- 2:41Speaker 2: That's a great analogy.
- 2:42Speaker 1: Yeah, And the first category the scientific reports. That's like
- 2:45Speaker 1: handing raw ingredients to a chef in the kitchen to
- 2:47Speaker 1: see what dish they bring back out to you.
- 2:50Speaker 2: Exactly. It's either a catalyst for generation or a token
- 2:53Speaker 2: for access. And the specific scenario we're going to unpack
- 2:57Speaker 2: today really illustrates that generation concept. We're going to mentally
- 3:01Speaker 2: walk through the automation of a reverse image search.
- 3:04Speaker 1: Okay, so like uploading an image to find similar or
- 3:08Speaker 1: duplicate images scattered across the Internet. Let's hear we're using
- 3:11Speaker 1: a platform called tenny.
- 3:12Speaker 2: Right in that scenario, we are taking a local image file,
- 3:15Speaker 2: passing it to Tenny's search engine and forcing the server
- 3:19Speaker 2: to return a list of every single domain hosting a
- 3:22Speaker 2: duplicate of that image.
- 3:23Speaker 1: Well, which sounds simple enough, but to actually execute that,
- 3:26Speaker 1: we have to talk about the tools we're using, the
- 3:28Speaker 1: automation toolstack.
- 3:29Speaker 2: Yes, the division of labor here is critical. We're looking
- 3:32Speaker 2: at a specific partnership between two primary technologies, Selenium and
- 3:37Speaker 2: beautiful Soup.
- 3:38Speaker 1: Okay, so Selenium and beautiful Soup, let's break those down. Sure.
- 3:41Speaker 2: So Selenium is functioning as our active webdriver. It utilizes
- 3:46Speaker 2: the W three C webdriver protocol to interface directly with
- 3:50Speaker 2: the browser's rendering.
- 3:51Speaker 1: Engine, basically acting as the driver of the car exactly.
- 3:55Speaker 2: It opens Chrome, maximizes the window, navigates to the website,
- 3:58Speaker 2: handles the JavaScript execution. It mimics the act of human interactions.
- 4:02Speaker 2: It's the engine getting us to the data.
- 4:04Speaker 1: Okay, and beautiful Soup.
- 4:06Speaker 2: Beautiful Soup acts as the extractor. Once Selenium has forced
- 4:10Speaker 2: the server to render the search results, beautiful Soup steps
- 4:13Speaker 2: in to parse the generated dom and pull up the
- 4:15Speaker 2: website names.
- 4:16Speaker 1: Wait, hold on, why do we need two different tools
- 4:18Speaker 1: for that? Yeah, I mean if Selenium is already in
- 4:21Speaker 1: the browser, manipulating the page. Why pass the tot at all.
- 4:25Speaker 1: It's a fair question, Yeah, because doesn't Selenium have built
- 4:28Speaker 1: in methods to locate elements and extract text. Why introduce
- 4:32Speaker 1: a whole secondary library just for parsing.
- 4:34Speaker 2: Well, you're basically trading Selenium's heavy, slow browser context for
- 4:39Speaker 2: Beautiful Soup's lightweight in memory parsing.
- 4:42Speaker 1: Ah, So it's a speed issue exactly.
- 4:44Speaker 2: Selenium is incredibly powerful for interaction, right, but extracting large,
- 4:49Speaker 2: deeply nested dom trees using webdriver commands is computationally really
- 4:53Speaker 2: expensive because.
- 4:54Speaker 1: It has to talk to the browser every single time.
- 4:56Speaker 2: Yes, every time Selenium queries an element, it makes an
- 5:00Speaker 2: HTTP request over the web driving protocol. So if you
- 5:03Speaker 2: are scraping you know, thousands of image source URLs from
- 5:06Speaker 2: a results page, those network requests create a massive bottleneck.
- 5:11Speaker 1: Okay, I get it now. Beautiful Soup is brilliant at
- 5:13Speaker 1: parsing through HTML, but it's totally passive. It can't click or.
- 5:17Speaker 2: Type, right, it can't interact at all.
- 5:19Speaker 1: So once Selenium forces the site to load everything and
- 5:22Speaker 1: generate the final HTML, it just grabs that raw page
- 5:26Speaker 1: source string and hands it directly over to Beautiful soup.
- 5:29Speaker 2: Yep, Beautiful Soup doesn't care about network latency or rendering.
- 5:33Speaker 2: It simply takes that raw string, builds it in memory tree,
- 5:37Speaker 2: and lets you traverse thousands of nodes in fractions of
- 5:40Speaker 2: a second.
- 5:40Speaker 1: So Selenium navigates the physical space and Beautiful Soup digests
- 5:44Speaker 1: the mathematical structure that makes perfect sex.
- 5:46Speaker 2: It's a very elegant division of labor.
- 5:48Speaker 1: Okay, so we have our tools. We've navigated to the
- 5:51Speaker 1: Tenney Reverse Image Search homepage using Selenium. The page is loaded.
- 5:56Speaker 1: Now we need to actually find the upload module. Right.
- 5:59Speaker 2: This is where we hit the absolute biggest technical trap
- 6:02Speaker 2: in web automation.
- 6:03Speaker 1: The OS trap. Let's dig into this because this is fascinating.
- 6:06Speaker 2: So to automate the upload, we first inspect the website's
- 6:09Speaker 2: code to find the upload form. And any element capable
- 6:12Speaker 2: of receiving a file is anchored by a very specific attribute.
- 6:15Speaker 1: What are we looking for?
- 6:16Speaker 2: We're targeting an HTML input tag with the attribute type equals.
- 6:20Speaker 1: File type equals file got it.
- 6:22Speaker 2: That is the universal structural beacon. I mean, regardless of
- 6:26Speaker 2: how much CSS is layered on top to make it
- 6:28Speaker 2: look like, you know, a friendly little dragon drop cloud icon.
- 6:32Speaker 2: The underlying HTML must contain type equals file.
- 6:37Speaker 1: Right, and in this specific tenny scenario we're picturing, let's
- 6:40Speaker 1: say this element also has a name attribute, and the
- 6:43Speaker 1: name is simply image.
- 6:45Speaker 2: Exactly, So we use Selenium's find element by name function.
- 6:49Speaker 2: We pass it the string image, and Selenium locates the
- 6:53Speaker 2: exact node in the DOM.
- 6:55Speaker 1: Okay, awesome, we found the button, so we just tell
- 6:58Speaker 1: Selenium to click it right?
- 6:59Speaker 2: No, right there? Wait, why if you tell the script
- 7:02Speaker 2: to click that button, the whole automation process breaks instantly,
- 7:06Speaker 2: which is.
- 7:06Speaker 1: Exactly what we were talking about at the very beginning
- 7:08Speaker 1: of the deep dive.
- 7:08Speaker 2: The freeze, Yes, the catastrophic freeze.
- 7:11Speaker 1: But why, I mean Selenium click buttons all the time.
- 7:14Speaker 2: Because clicking an upload button launches the operating system's native
- 7:18Speaker 2: file selection dialogue box. You know that familiar pop up
- 7:21Speaker 2: where you browse your computer's folders.
- 7:22Speaker 1: Oh right, the little Windows or macfile explorer window exactly.
- 7:27Speaker 2: And because that file explorer is a native OSGi element,
- 7:31Speaker 2: Selenium has absolutely no hook into it. Oh wow, yeah,
- 7:34Speaker 2: Seleneuman can only interact with elements inside the HTML DOM,
- 7:38Speaker 2: the document object model of the web page. The webdriver
- 7:41Speaker 2: protocol communicates with the browser, not the Windows API or
- 7:45Speaker 2: the macOS window manager.
- 7:46Speaker 1: So the script sends the click command, the browser fires
- 7:50Speaker 1: the event, and then the operating system just violently hijacks
- 7:53Speaker 1: the focus of the machine.
- 7:54Speaker 2: Yes, the OS takes over to render the native file dialogue,
- 7:58Speaker 2: and Selenium is now totally trapped.
- 8:00Speaker 1: Because it's waiting for the browser to say, hey, the.
- 8:03Speaker 2: Click is done, right, But the browser is suspended waiting
- 8:05Speaker 2: for the user to close the OS dialogue box. The
- 8:08Speaker 2: execution context is completely deadlocked.
- 8:11Speaker 1: That is such a brilliant illustration of the boundary layers
- 8:13Speaker 1: and computing. I mean to conceptualize this. Think about it
- 8:16Speaker 1: like a restaurant. The dom of the web page is
- 8:18Speaker 1: the dining room. Okay, I like this, and Selenium is
- 8:20Speaker 1: our waiter. The waiter knows the coordinates of every table,
- 8:24Speaker 1: every chair, every menu item in that specific dining room.
- 8:27Speaker 1: They have total jurisdiction there.
- 8:29Speaker 2: But the operating systems file explore dialogue box, well, that's
- 8:33Speaker 2: the kitchen exactly.
- 8:35Speaker 1: The OS is the kitchen, and our waiter is strictly
- 8:38Speaker 1: front of house. So if we make the waiter push
- 8:41Speaker 1: the button that opens the kitchen doors, the kitchen staff,
- 8:44Speaker 1: the operating system takes over the interaction entirely. Our waiter
- 8:48Speaker 1: is allowed in the kitchen, so they're just left standing
- 8:50Speaker 1: at the swinging doors, totally frozen, waiting for a dish
- 8:53Speaker 1: that they literally cannot reach in and grab themselves.
- 8:56Speaker 2: That is a perfect analogy. It perfectly isolates why visual
- 9:00Speaker 2: automation models fail. You see, visually, a human user sees
- 9:04Speaker 2: the browser and the file explorer as one continuous workflow.
- 9:08Speaker 1: Yeah, you just click and pick your file. It feels seamless.
- 9:11Speaker 2: Exactly, But structurally you are crossing a hard boundary between
- 9:15Speaker 2: an isolated web application and the local machine's native environment.
- 9:19Speaker 2: The script cannot cross that boundary.
- 9:22Speaker 1: It has to circumvent it.
- 9:23Speaker 2: It absolutely must.
- 9:24Speaker 1: Okay, so how do we actually do that? I mean,
- 9:25Speaker 1: how does a developer bypass the OS entirely? If we
- 9:29Speaker 1: can't click the button to trigger the menu, how do
- 9:31Speaker 1: we actually get our local file onto the website?
- 9:34Speaker 2: We exploit a developer bypass. We use the fact that
- 9:37Speaker 2: an input node in the DOM is essentially just a
- 9:40Speaker 2: container for a value. Okay, we don't need the graphical
- 9:43Speaker 2: interface to populate that value. We can use a very
- 9:45Speaker 2: specific webdriver function called send keys.
- 9:48Speaker 1: Wait send keys, but typically isn't send keys just used
- 9:52Speaker 1: for injecting strings into text fields, like typing a word
- 9:55Speaker 1: into a search bar.
- 9:56Speaker 2: Usually yes, but we are co opting it here. Out
- 10:00Speaker 2: of clicking the button to open the visual folder menu,
- 10:02Speaker 2: we use send keys to directly inject the absolute local
- 10:06Speaker 2: file path of our image straight into the website's code.
- 10:09Speaker 1: Oh wow, so we're bypassing the interface entirely.
- 10:12Speaker 2: Entirely, we intercept the dumbnode and directly manipulate its value property.
- 10:16Speaker 1: So, practically speaking, let's say we have an image saved
- 10:19Speaker 1: on our computer, like a plural site logo or something.
- 10:22Speaker 2: Right, we take the absolute local path of that target image,
- 10:25Speaker 2: So say seed drive slash downloads slash plural site logo
- 10:29Speaker 2: dot jpg, and.
- 10:30Speaker 1: We just passed that raw strain directly into the send
- 10:32Speaker 1: keys function targeting that type equals file element.
- 10:35Speaker 2: Yes, the moment that string is injected, the browser's internal
- 10:39Speaker 2: engine validates the path against your local file system.
- 10:42Speaker 1: And if the path is real.
- 10:44Speaker 2: If it resolves, the browser just attaches the binary data
- 10:47Speaker 2: of the file to the form completely bypassing the os
- 10:50Speaker 2: file dialogue. The native pop up literally never even.
- 10:54Speaker 1: Renders, it just silently uploads it in the background.
- 10:56Speaker 2: In milliseconds, we simulate hitting shift plus enter and boom.
- 11:00Speaker 2: The form submits we have successfully pushed a local binary
- 11:04Speaker 2: file into a remote server using nothing but a raw
- 11:07Speaker 2: string injection.
- 11:08Speaker 1: That feels like a magic trick. You're no longer constrained
- 11:11Speaker 1: by the visual interface designed for humans. You're just operating
- 11:14Speaker 1: purely on the underlying.
- 11:15Speaker 2: Architecture exactly, and this triggers the handoff we discussed earlier.
- 11:19Speaker 2: Once the logo is uploaded to tenny, the reverse image search.
- 11:22Speaker 1: Runs right, The processes it and serves back the results page.
- 11:26Speaker 2: And now Selenium's job is done. Its execution context is fulfilled.
- 11:30Speaker 2: It pulls the raw page source of the results, passes
- 11:33Speaker 2: it into Beautiful Soups in memory space, and the parser
- 11:37Speaker 2: extracts all those duplicate website addresses instantly.
- 11:40Speaker 1: The efficiency of that whole pipeline is just remarkable.
- 11:44Speaker 2: It really is. But you know, the core takeaway here
- 11:47Speaker 2: isn't just about combining Selenium and Beautiful Soup. It's about
- 11:50Speaker 2: recognizing system boundaries totally. By understanding why that OS trap occurs,
- 11:56Speaker 2: you develop an instinct for how data actually moves between
- 12:00Speaker 2: different environments.
- 12:01Speaker 1: It really does change how you look at every single
- 12:03Speaker 1: button on a web page. When you see an upload field,
- 12:06Speaker 1: now you shouldn't see a button, you should see an
- 12:08Speaker 1: injection point exactly.
- 12:09Speaker 2: You start thinking like a developer.
- 12:11Speaker 1: Which brings us to a quick review question for you,
- 12:13Speaker 1: the listener, to kind of reinforce what we've covered today.
- 12:17Speaker 1: Next time you are on a website that asks you
- 12:19Speaker 1: to upload a file, I want you to picture the
- 12:23Speaker 1: underlying code right, trace the execution path. Ask yourself what
- 12:27Speaker 1: specific HTML attribute represents that upload button and what specific
- 12:32Speaker 1: function would your automated script use to bypass the operating
- 12:36Speaker 1: system's pop up menu.
- 12:37Speaker 2: I'll give you a hint. It's the difference between a
- 12:40Speaker 2: brittle script that hangs randomly and a robust, headless automation pipeline.
- 12:45Speaker 1: Exactly identifying type equals file and applying send keys. That's
- 12:50Speaker 1: the secret.
- 12:51Speaker 2: It really is the cheat code.
- 12:52Speaker 1: But you know, looking at the sheer speed and invisibility
- 12:56Speaker 1: of that bypass, it leaves us with a pretty profound
- 13:00Speaker 1: architectural question.
- 13:01Speaker 2: Oh absolutely, we just.
- 13:02Speaker 1: Outlined how effortlessly a script can bypass the native OS
- 13:06Speaker 1: dialogue and inject a file path directly into the dom
- 13:10Speaker 1: nodes value property. It requires zero graphical interaction.
- 13:14Speaker 2: It is entirely programmatic and silent.
- 13:16Speaker 1: Right, So, if you are engineering the back end of
- 13:18Speaker 1: a secure portal, how do you defend against that?
- 13:21Speaker 2: That is the million dollar question.
- 13:23Speaker 1: If the native OS dialogue is completely circumvented, how does
- 13:26Speaker 1: the platform actually know if a human painstakingly navigated their
- 13:30Speaker 1: local directory to upload a picture or if a bot
- 13:33Speaker 1: injected it in a fraction of a millisecond.
- 13:35Speaker 2: They often don't. The system just assumes the boundary is secure.
- 13:38Speaker 2: It assumes the browser's graphical interface is the only point
- 13:41Speaker 2: of entry.
- 13:42Speaker 1: And when you strip away that interface, you expose the
- 13:45Speaker 1: raw mechanics of the interaction. It really makes you wonder
- 13:48Speaker 1: how much of modern web security relies on the assumption
- 13:52Speaker 1: of human friction and what happens when bots automate that
- 13:55Speaker 1: friction away.
- 13:56Speaker 2: Entirely something to definitely ponder on your own.
- 13:58Speaker 1: Keep looking closely at the architecture around you till next time.