Now, one of the best ways, in my opinion, to actually use this soup object is to use the select method
on it.
And Select allows us to grab a piece of data from the soup that we just downloaded and created using
what we call a CSS selector.
Now this is a python course, so we're not going to focus too much on CSS selectors.
I will link to a resource so you can learn more about them.
But a CSS selector allows us to access this data.
Using well CSS selectors.
So if I Google here CSS selectors you see here that well it's a list of different ways to grab elements
on an HTML page.
We can use the class, the ID, the actual element.
So let me show you how to do that.
I can say, for example, select a.
And what is this going to do?
Well, if I run this script, it's going to select all the attacks.
That's nothing new.
We've seen that before.
We can do that for Div as well if we want it to.
But we can go beyond just an element and say we want to select, let's say, the class score.
So this class score can be accessed by using the dot notation.
So dot score is going to grab all the scores on the page.
So watch this.
If I clear this and run here.
Look at that.
I grabbed all the spam tags that have scores on them.
So you see over here that this is just a list of all the spans, because this span has a class of score.
This span has a class of score.
And we can do the same thing with IDs.
So if you wanted to grab this ID, we simply say instead of a dot which stands for a class, we do LB
and then the ID.
So if I run this.
We get.
This one span because this is the only one with this specific ID.
Now I'll link to a resource where you can learn about more CSS selectors.
They become really, really powerful when you start selecting different, complicated, nested elements.
Let me show you.
Let's start to plan out what we want to do.
Now in here.
I'm going to close this for now.
I want to grab the link over here.
So that is the title and the link of a story that has more than 100 points, votes and everything else
that doesn't have 100 points like this one I just don't care about.
So I'm just filtering it out.
So what we want to do is, first of all, grab this link and grab this votes.
So we also list out how many votes this story has.
So the way I'm going to do this is I'm going to right click, click, inspect.
What we know is that we want to grab this link that has a class of story link.
Okay.
So just to double check if I go to this one over here and inspect.
You see that here?
This one has a class of story link as well.
So let's just grab all the elements that have class story link and see what happens.
I'm going to say dot story link because dot means it's a class.
And let's see what we get.
I'm going to run the file.
And all right, we get a ton of lists.
But it looks like they're all attacks.
So these are all links that we can use.
It looks like this is a list, so we can probably grab it using the square bracket zero to grab the
first element.
Let's see if this works.
I'm going to hope I'm going to clear here.
And run this again.
All right.
Look at that visual information theory, 2015.
That's the first item in here.
So it looks like story link grabs us the tags, the links for each one of these items.
Okay, what about votes?
Let's grab the votes as well.
I'm going to right click, say inspect.
And what do we have here?
We see that we have class of score, so let's just grab all the classes.
So I'm going to say that this is going to be links and that is well, just instead of grabbing the first
item, I'm going to grab all the links and I'm also going to grab all the votes.
And all the votes is going to be snoop, dot, select and then grab.
What was it again?
It was the score class.
So I'm going to say grab all the elements that have the score class.
And just to make sure that this works, let's just say print votes and grab the first item.
So I'm going to run this.
And look that I have 180 points because the first story.
Well, let's refresh the page, because this may have changed.
Yeah, it looks like.
See, this website is constantly changing.
It now has 182 votes.
So if I refresh here, there you go.
182 points.
Awesome.
So we now have grabbed the two pieces of information that we really care about, the links and the votes.
And luckily they're all in order because each link, the first link and the first vote is together,
the second link and the second vote is together.
So that's useful for us.
Now, just as a side note with beautiful soup, we can keep changing these, right?
For example, I can now say votes.
Dot get and get, for example, an attribute name.
So, for example, I can grab.
A score over here and say, hey, grab the score from here.
So that if I do print here.
And I run this.
Well, votes is a list, so we actually have to grab the first element.
So let's just grab the first element.
Run this again.
And we get none because, well, this is the value.
The attribute is ID.
So if I do get.
ID, for example, and I run this.
I now get the actual ID value.
So with beautiful soup we can keep changing these, we can keep selecting.
So I can say dot select here if I want, if we have even more information.
But in our case we have the information that we need.
It's time for us to go to the next step, which is to try and combine these and make it more useful.
So let's do that in the next video.