In the final demo for this course, we'll do a reverse image search.
What is a reverse image search?
Reverse image search or website essentially means
when you upload an image to it,
it shows you similar or duplicate images that are
scattered throughout the internet.
And what we'll do in this demo is use Selenium to navigate the
website and control the image upload process.
And then,
we'll use Beautiful Soup to extract the website names
in which the same image was found.
The website we'll use for this process is called TinEye. Let's get to coding.
The first three code blocks must be familiar to you by now.
We import the libraries,
add options to display Chrome in maximize mode, and then
navigate to the website. Within the website, this button
is used to upload the image.
So, we right‑click on it and click on Inspect. Every
upload form on any site has a file input.
You can easily find it by looking for an input element
that has the type = file attribute, which you can find here.
The name for this element is image.
So we use find_element_by_name function to locate the button element.
Now the instinctive thing to do here is to use click on it, but
you'll end up with a problem. Doing so will launch the dialog
box to select the file we want.
You'll then end up getting stuck because Selenium can only interact with
elements from inside the HTML DOM, or document object model.
The dialog box prompt is part of the operating system and not the DOM.
So what do we do? We use the send_keys function,
the same function we used to enter text in the earlier demos.
In the function, we provide the local path of the file,
which is what I've done here. Press Shift + Enter, and there you go.
The image we used is that of a Pluralsight logo.
Obviously, you can use pretty much any image you want. You can see that the
image of the logo exists in a whole lot of places as expected.
Now that the upload process is done, Selenium is no longer required.
So let's get the page source information from the driver,
which we then pass to Beautiful Soup for further processing.
Now, we want to extract the address of the websites that contain the
logo. So, we right‑click and inspect one of them.
And this code here helps us extract the website addresses.
There you go.
We've successfully uploaded a file and then extracted generated information.