In this lesson,
I have another super quick challenge for you
so you can practice setting up the Selenium webdriver
in a blank project and scraping a different piece
of data from a website.
This time we're going to work with the Wikipedia main page.
So you're going to head over to the Course Resources
and find the link to this page,
or you can just type it in.
Back in our project, I'm going to create a new file
and I'm going to call this interaction.py.
Now, in this new Python file, we're going to interact
with this Wikipedia webpage.
And as a challenge to you, the first thing I want you
to do is to figure out how you can get a hold
of this particular number
and print it out inside our interaction.py.
Remember that you'll need to import Selenium
and also use the webdriver to get hold of this page
and then find this particular number
and finally print it out.
And then when you're ready to run it, all you have
to do is right-click
and then Run this interaction.py and it'll work
and you should see the outcome being printed
in your console.
So pause the video now and give that a go.
Alright, so here's the solution.
First, we're going to go into the Selenium package,
which we've already installed into this project
so we don't have to install it again.
And then we're going to import the webdriver.
Now using the web driver, we're going to create a new driver
from the Chrome browser,
But this is what we put to initialize a new Chrome driver.
Now once we've created our driver,
now we can use the driver to navigate to our webpage,
which is done using get().
And this is the URL, which we'll copy and paste into here.
And once we've gotten hold of this page, then we're going
to try to narrow down on this particular element.
So let's go ahead and Inspect it.
And you can see that it's inside an anchor tag
with no particular identifiers.
There's no id, there's no name, there's no class.
But this anchor tag lives in a div that has an id.
So this articlecount is going to be a unique identifier
for the div that holds this particular anchor tag.
So we can narrow in on this anchor tag
using our CSS selectors.
So we can say driver.find_element(By.CSS_SELECTOR...)
make sure that it's element, not elements.
And then inside here we're going to put our selector,
which is first the id of articlecount,
and that is going to be proceeded with a pound sign.
And then inside that div with that id, we're looking
for the first anchor tag.
Now notice that inside that div,
there's actually two anchor tags.
But by using this find_element(By.CSS_SELECTOR...),
it's only going to give us the first one
that matches this criteria.
So this is going
to be our article_count.
And now what we want to do is we want to print
the article_count.text.
So now let's go ahead and right-click
and Run our interaction.py.
It should open up our browser to this page.
And now it should have found and printed out that number.
So this is what we've been doing so far,
creating our driver, opening webpages,
and then finding specific elements
and printing some sort of property,
but the next step is to actually form some sort
of interaction with the webpage.
For example, clicking on a link,
or typing something into the search bar,
because after all, when we're working with websites,
it's often that we'll need to interact with it in order
to navigate to new pages
and get hold of specific pieces of information
that we're interested in.
And that's what I'm going to show you in the next lesson.
So I'll see you there.