Extracting labels and handling curious exceptions.
Hi, guys.
Today, we're going to talk about how to extract links and deal with exceptions with this topic last
time.
But today we're going to extend it because I want to show some very nice features here.
So let's close this.
And here in our Section four, I will not lose any time and create a new item.
File in the Python file will be called exacty news.
OK, extracting news.
So let's start creating the file and I will run it after that.
So let's try Eurorail slash and then slash EMV item three.
OK.
And next, we're going to import some libraries in order to make possible the extraction of the news
from Internet completely automatic.
So let's now import requests, OK?
And then import once again.
Or actually I write from B is for import.
Beautiful.
So, OK.
And now let's create a function here.
This task will be done with functions, so the function will be kept.
From.
Page.
OK.
And then I will write.
I don't get equals h t Tepes.
Then double Dutch news.
OK, news that y c o m b natur dot com.
So this will be the website that will extract news and our right front page.
OK, this is a variable in the front page will be requests not to get and we're going to get to the
euro or the target.
OK, let me fix this spelling mistake here.
OK, so let's now create an if statement.
So if not from page, OK, then RES runtime error and the runtime error will be Kante.
OK, Kent.
Assess the news.
OK, and then let's try the new underscore, so.
OK.
And this will be equal to beautiful soul, so here is an object from the beautiful subclause and our
right front.
Page, dot text.
OK, front page text and then comma, and obviously here I write L XML for the protocol and here will
return from the function nu so ok.
So obviously this function will not take any parameters, but it will return the beautiful sope element
which will be caught in your soul.
And obviously after that in the next functions we're actually going to use what the dysfunction returns,
namely a beautiful sope element.
So now I would create another function which would be called find links.
OK, and here I write.
So, so this parameter that will need to pass and I write here items equals so dot find all and then
I'll write to the.
Cuomo.
And here I will create a dictionary.
So right here, I'll line the two dots, right?
And after that first, right, Cuomo, the last, OK, and then.
Title, that's it, very good.
Now let's write links.
We be cool to this, and that's right for I mean, items.
Mm hmm, that's right.
And here again, we're going to try some exceptions, so our right siblings EQUASS list and then I dot
next siblings.
Mm hmm.
And then also underscore, Heidi, equals.
Siblings Wyandot find.
And first, I will look for A and I d.
OK, then let's try to link equals siblings and hair right to that find.
And here I'll look for something different, actually.
So let's right here in the brackets, actually let's write as well, because we are going to look for
elements of what a hero and I'll write h are you OK?
Now let's write title and for the title will write against siblings to dot text.
OK, since that letter was a text and the links that are bent and let's write here link and then link
Cómo Bitel and then here title command post.
I did not see it and then let's write post.
I did OK and let's close the bracket here, which I actually hit upon here before the first curly bracket.
OK, so this is a list with the links.
And obviously, as you can see earlier on, we created this links list and now here for AI in items
which are those, we're going to search for those items and we're going to upend the items into this
list, into the link list.
So we're going to open the link at this one.
We're going to open the title that we found here and we're going to append the post ID, OK?
And as you can see here, the exact parameters and we set the keys exactly the same name.
OK, and let's right here, except exception us the OK and plus OK.
And then let's right here return links.
OK, so guys, since now we wrote the two functions, let's create just on the right the main function
that we're going to write today.
So here I will write if name equals.
Maine, actually, Maine.
That's it.
So if the name is Maine, so the function is the main function, then they are right so equals get front
page.
OK.
So as you remember, this is the function that you wrote earlier and then the results.
So resorts will be called to find links and they'll pass here.
So that is the output actually of the previous function, as I told you.
So the output, the new soap here in the main function we refer to.
So which comes from the first function and then the beautiful set of the beautiful sole object will
be passed actually to the next function.
And we will get our results here.
And finally, because we would like to print something on our screens, our right for error in resorts.
OK, we all right if our is not none.
So our has to have a value print.
Our.
And in the brackets, I referred to the link.
OK, Luss.
Space Los Angeles, right, are litho, actually, we put it in parentheses, that's it, and we're
done guys, with the main function.
So let's go now to the terminal and let's run our code.
So first of all hours and actually save it.
That's it.
So if we write now Python underscore the underscore news, not way.
Let's run it and let's first see.
OK, so here we have some error.
Let's see what is in the defined links.
Yes.
Here we need to have a semicolon.
Let's save it and let's go back to the terminal.
So, OK, we can see that we don't have any errors and you can see that now, guys, we've got all the
links here with all the news.
And you can see that we've got quite a lot of them.
And actually, let me copy the link of some of the news and run it and see what's going to be there.
Obviously, hear what you're getting is the link.
And also you are getting the header.
So you see the date of the online search hardware accelerators and so on.
These are all headers over the pages.
So if I run those, you can actually see that there is some article here and you can see that the header
is the date of the online search and blah, blah, blah.
So here's how you can assess the news page essentially, and to have all of them for your own purposes
just by using Python in beautiful soul.
So that said, guys, this will be the end of this video, but continue with the section because I have
quite a few more interesting sections for you.
And we're going to write a lot of code and really enjoy using Python for networking on the Web.
That's it.
Thank you very much for watching.
And I'll see you in the next video.