Getting pages and images with XPath, hello, everyone.
Today, I'm going to teach you how to get images and pages using this expert.
So to do that, you can create first a single file that will actually get you some links, images once
you run.
So let's create a new file here.
And this will be called GET.
Imagists.
Let me just fix this big mistake.
Images.
That's it.
So let's right here on the top yuzu us are been in three Python three.
OK, and let me actually fix this warning so it doesn't bother us.
And let's start importing the thing.
So let's import and let's import OS and then let's import another package, the requests.
And after that we'll simply actually I will do from so from El Ximo import HMO.
OK, and then let's create now a class and this class will be called scrapping.
So I will do a class scrapping.
OK.
And then.
That's right.
ADF scrubbing images and then self colma you are.
OK and let's start building our new class so let's try to first print and the first things I would like
to print is.
To write to the user and getting images from Yorio and then the hero that we want to place so out of
print, and then here I will do backslash and and then getting images from the URL, OK?
And after that, let's try.
Plus you rl.
That's it.
And then let's write three.
And here, right, response equals requests that you get Eurorail.
OK?
And this Eurail here that you see it will store actually the variable of the bureau that you write to
the system.
So once you're on the court, the program will ask you for a euro.
And once you select the euro, this will be replaced in the code with the euro that you see right here.
So let me actually get a bit closer to you so you can have a better look.
So let's right then ask the body.
So I'll do past volume parameter and this will be equal to H.M. Dot.
From starring in Hero, right, the wrist, bones, wrists, bones, that text.
OK, and then I will write images equals.
Past underscore body, not exports, and then I can do limited to the slashes I m g led to add a Sarsae.
OK, so this is our regular expression, guys, for getting the images.
OK, now after we get this, let's print found images.
Percent is, and then we'll find the length of the images or how many images we have so as to learn
OK in the brickies images, that's it.
OK, then let's right always dot system.
And then make their images and, you know, Mercader, it's actually creating story, and this is because
once you get all those images from the Web address that you specify, you actually don't want to place
them on random places or just to the folder that you're in.
So instead of that, you can create a folder with a name and add all the images there.
And this is what we're doing right now.
OK, so once we were ready without that, let's create a for loop.
So for image in images.
Let's do if.
Image that starts with age TTP, so if the image others start with HTP, false.
OK, and then our right write double world equals U rl plus slash plus image.
OK, so this how it will be displayed once we start downloading and then our right else download will
simply be equal to image.
OK.
If it doesn't contain the HTP.
So we do this in order to have all the images in the same format.
So let's do print and then.
Well, OK.
And this is how we don't know the images in the Images Directorate.
Now, after we do this in the same indentation level, let's write our equals requests that you get
downloads, OK?
And.
And then that's right, if F equals to open Imagists, slash percent is.
And then they were right here, percent don't want that split.
And here, let's write a slash and then minus one.
Como and WB, and let's close the record here and here, there's an error because I didn't have to put
this bracket here, so just remove it.
So we have a single bracket close here and one close here.
OK, that's fine.
And let's right now, if dot right.
Our dot content, OK.
So as you can see, we're getting the request and authority and our value from the foul.
Don Walt.
OK, which is actually the actual image or the actual images, because it's iterative.
So for every image, we are going to do that and we're going to store specific value content.
And then there's the evidence close.
OK.
And that's it.
Um, so let's go out of this for a loop and let's go out of the Trelew here and let's try it now.
Another exception.
So except exception E!
And then let's to print.
And then Conexion.
Error in.
And then let's try to the euro plus euro.
OK, that will pass.
So this is just in case we have a connection there and then I will pass.
So we don't get an actual systema.
So whilst we are the guys with the scrapping images function or the way that we're actually getting
the images, let's go out of dysfunction here and let's go to the next one.
Which section away shorter, so the next one will be G.F. scrapping.
Linc's.
OK, scrap interlinks, and this will be ours cracklings function and to also take the euro that we
owe us.
So let's write print.
And here I am, right, OK, because again and then getting.
Links from your URL.
OK.
Plus your URL.
OK, and then I will do try and what we're going to try here is response equals requests that we get
and we're going to obviously get the euro here.
We already have practice with that.
And then I write.
Again, past body equals H.M. Dot.
From strain response that.
Text, OK?
So we actually get the website and we transfer it from e-mail to string.
OK, and now let's write that the regular expression to get the links.
So our simple right links.
He, of course.
Asked.
Body dot expert, and here I will do a slush slush, a slush.
At.
H r e f.
OK.
And you can see that there are slight differences between how we get the links here and how we get the
images here.
OK, because here we get with AMG Slash at sea, our SIRC.
And here we we're getting the links by a H are if.
OK, so this is the regular expression for that, for getting the links then.
That's right.
Print.
And outright hero found Linc's percent as and to the percent who were actually going to pass the number
of links, which is LENTH.
Links.
That's it.
And Hiroto for Loop, so for link in links perent link, actually this is quite straightforward here,
so I'll just link.
OK, that's it.
And let's go away from this trial law and I will do except exception.
Oops.
Sorry, the wrong one exception that set us e print.
So if the previous line of code does not work, we would like to print.
Connection, error in.
And then I was the link, you know, that's it, and then that's right.
OK, so I'm just going to remove Darby here because I think this a typo.
It shouldn't be scrapping with the double paper just with a single one.
So that's it here, guys, with dysfunction.
And let's finally.
Created the main function, which will actually run the code, because, as you know, we just created
a class here.
Which is also it single, by the way, and let's try it if.
Name equals main, and this is standard running function here.
So if that's the case, let's try target equals.
And here I will place a website with Hacker News.
Actually, it's quite interesting.
So it's called News Dot.
Y Combinator dot com.
And then let's do.
Scrapping equals.
Scrapping, that's it, and then.
Scrapping the dot, scrapping images, and then I will do target.
OK, so this will be the euro that will be using this one.
And after that, let's do another one scrapping DOT.
Let's try scrapping links, and our target will be the target file.
OK, we're ready here.
And let's go a little bit up because I found some typos in the previous function.
So this here is not so selfish yourself.
And also here starts with instead of start with.
So let's save these guys and let's now go to our terminal.
We run the files and let's try it Elway's.
And you can see that now you can have the file, get images.
So let's run.
Let's try Wighton and get images of your way.
And OK, here I can see that there was some error.
Let's go back and fix it.
Line fifty nine.
So yes, here I missed the hour and here I am.
Is that again.
OK, let's save that and let's go back.
So let's run it against corruption on.
Yes.
So here is scrapping.
That's it.
OK, so now all the files are the same and let's run the code once again and see if everything's OK.
So you can see that now, guys, everything is good.
And we've got quite a few links and you can see how many actual links and images we got from this website.
And actually, if you check the Section four and I will just put up for you, you can see that we've
also created the images folder and we've got some images here from that website, which are, of course,
not that impressive, but is still something to display there.
Let's actually change the name of the website and let's see what we get.
So if I do hear You Tube dot com, actually just You Tube and let's get back here and less from the
code.
And you can see that now you started getting more links and images and you can see that you go to the
YouTube politics developers, DOT, Google, YouTube and so on.
And this is just because we change the website.
So this is how you can actually automate how to get all the links and images from a website using specifically
this tool called Expert Expressions.
I hope you enjoyed that video and the lectures until now.
So I will be waiting for you in the next ones where we're going to start working with a very interesting
tool called Beautiful Setup.
Thanks for watching.