Hi, welcome to a new lecture here you learn how to use beautiful soup, a very important web scraping
of library for Python.
No, we use selenium through this section.
And why are we using beautiful soup?
And what's the difference between selenium and beautiful soup?
Well, selenium is more targets for browser automations, which means you can use selenium to automate
actions on a browser, such as feeling text boxes, clicking on a button, logging in and out, and
so on.
So you can use browser automation to scrape values than or log in and into actions.
But if you want to just extract dots from a website, you don't have to open the browser like you do
with selenium.
So for that purpose, we can use beautiful soup, which is a more straightforward approach to web scraping.
So you can think of selenium as a hybrid between browser automation and web scraping.
So it does both and think of beautiful soup as a library exclusively for web scraping.
So for this lecture, we're going to extract, we're going to scrape currency rates, the real time
currency rate between any two currencies.
For that, we're going to use excretes dot com extension rates dot com.
So that's a web page.
You can go to currency calculator and then you will see the current currency rate between these two
current currencies, which is US dollars and Indian rupee.
So the first thing you do is you want to specify manually on the browser what currency rates you want.
So currently, this is us three Indian rupees, if you want to change these to, for example, the Euros
to Indian rupees.
The page will refresh and you will get the rates for euros to Indian rupees.
Now, the browser address bar also changed to another URL.
So now it's as you can see from Euro to INR, which is the currency code for Indian rupees.
So any time you change this, let's say from Euro to USD, the browser with points to another URL,
so any currency rate has its own URL, and we can make use of this to access the value for any euro.
So for any conversion between any currencies, all we have to do is just have a string in Python or
this URL and then change, for example, EU or to USD and the two part to I and our or other exchange
currencies.
And so we get the value for any currency and then we extract that value from the webpage.
So let's do that.
I'm going to create a new empty report, you can do this on your local I.D..
Script currency with beautiful soup, so beautiful soup is the name of the library.
Let's go ahead and imported from B.C. for import.
Now best for is how you how you use beautiful soup, but the way you install it is, you go to the terminal
or to the shell on Ripple and you see paper or paper three point nine or three points in whatever you're
using.
Install.
View tea for soup for beauty, I hear.
Execute.
Once you have installed, it's a van you imported as from the US for import, a beautiful subclass event,
the way I approach this is I will write a function defined gets currency and this function is going
to get inputs, currency and an ultimate currency.
So two arguments.
So this will be the currency code, so you as the EU or I and cetera.
And then inside this function, I'm going to construct a euro, which is going to be have strings.
So basically, I'm going to copy this euro and paste this in there and then instead of you or I will
have a placeholder.
So let me expand this like that.
So inside there we'll go the input currency entered by the user.
So from that currency to instead of USD, I'm going to have alt currency, so two variables.
So let's test that.
I'm going to bring the URL and just call the function here, gets currency, enter, for example, I
are here and you you as the and then execute the Python file.
To see if the URL is being constructed correctly.
So that's the URL we get.
If I click that.
We're going to get the rates here.
Silver Page from Indian rupees to USD.
So from Indian rupees to the US, the and that was the rate right to know that we have, you know,
correctly constructed.
Then we can go ahead and get the content.
And the source code of this web page for source codes is on a web browser.
You can find the source codes by Rudds clicking over the page and then go to show page source.
And I need to expand this.
So that's the source code that's constructs this Web page.
It's a combination of HTML and CSS and JavaScript.
So you want to access all that's called from Python, and we can do that using requests, so you need
to import requests.
And also install it if.
With PIP install requests, so with requests, we're going to get access to the page.
So the contents of the page, the source code is equal to requests that get you answer the other box
text and if you print out content and run the script.
We have this name error.
I forgot the type of year you are L instead of you are so that local variable.
And that's the source code that sees the same source code that we so hear on the browser.
No.
Out of that source codes we only want.
This number, so that is hidden somewhere in the source code, if I search for that's using this button
here.
Zero point zero one three three.
You see, it's somewhere in here.
We want to get that number only from that entire source codes.
So that number shows up when I search.
But if you want to go directly to the number going to right click here and go to inspect element.
Again, that would point to that.
You may need to expand this by clicking the triangle button.
And so that's a no.
Again, it's hidden in between these spam elements, HMO elements.
So I'll keep this winter open here and go back to my code.
No, the idea is that we need to pass that source code and this is where beautiful soup comes in.
So beautiful soup is designed to understand HCM mouth structure like this.
It knows it has knowledge of both elements and how a natural page is constructed.
You know, that's it.
Has this div tags and these diff tags may have other elements, insides and so on.
So the next step is then to have a variable.
Usually it's called soup.
You can have the rights, you can call it whatever you like, but that is equal to a beautiful soup.
That's class.
So we're making a soup here, basically, by using the contents of that, we got via the request library
here.
The content goes there and you specify that we're talking about an HMO.
That's a parser that is the parser that's beautiful.
The soup is going to use because there are other languages such as example, if a page is based on that
language, but usually it's A. M. Parsing what we're doing, so simple parser goes in there.
And now if you print out soup just to see what it is.
Ron?
So this time the function will bring soup.
It's basically the same thing.
But this is a more specific object.
It's a beautiful soup object, so now internally, it has knowledge about what each of these elements
are.
So then you can do.
Soup, don't find and you specify what do you want to find?
So what we want to find is a spam element.
That's the Spanish element.
So it's inside this text, and no, there are different there are various Spanish elements in the source
code.
So we want to be more specific and say that we want the span element with a class of this value.
So I'm copying this with Control C and going in here.
And then you say class with underscore equal to that.
Collapse value.
So if we print out this ultimate, which I'm going to.
Puts it in a very both currency and then I print out of currency and we see what we get.
So you see that with that, we got the entire elements, including the class of the elements and the
value that we won't know that this value is changing every certain time.
So you may not see the same value as you saw in here a minute ago.
So it's changing.
It's real time.
And so out of this, now we want to extract using the get tax methods.
We want to extract only the text only.
That's part.
So Ron.
And we get this and that is the current steroids.
No, this is a string.
As you may notice, it has this USB-C port, so it's good that we get this as a float without the USB
port.
But before we do the stripping of that string, let's first make sure that this function is working
by trying some other values here for the currencies euros to something else.
Let's see what we have a D for Australian dollars to run.
Yeah, that's the current rates for that exchange.
It seems to be working now.
Let's clean out that.
Strained and gets only the flow, the number.
What we actually want is we want to remove the last four characters of the shrink, so one character
to three and the space between the number and the currency codes for characters.
To do that.
What I will do is say currency or I will rename this variable, actually.
Two reads, it makes more sense that is the rate, that's the rate.
So the rate is going to be and the current rate.
So we want from zero to.
So we're splitting the string from zero means we want the character with index zero, that is the character
who is index zero and then we want the Dot also and all the numbers, but we don't want the characters
from minus four.
So minus four means the space here.
So we have minus one for D minus two for you, minus three for eight and minus four is the index of
the space.
So from that to that, we don't want and we don't need a zero, this will do it.
So that's one.
If I run this this time, we're going to get the number only, but this is Typekit string.
If you print out the type of that's value you're going to see, it's on a C or so.
What we want to do also is convert it into a float.
And then you run it and then it's a float.
Now we can do math operations with that number, so it makes sense to return the correct to the delta
type because it's a no, it's not a string, actually.
Um, so finally, this function should return the rate.
That's and so if I run this, nothing is going to be printed out because we don't have a print function
anymore here.
So if you want to free notes something now, you don't put print functions in the function in your custom
functions.
That's not a good practice.
We did that because we were developing the function and we were looking at the intermediate results
we were getting, but now we want to return to that.
And then if you want to print, you say prints here.
So print the value of that function.
And to get that value.
Alternatively, you can see
current rates, a variable that is equal to vets, so current rates is now a variable that has the actual
clothes, the actual rates of the currency.
You can print outs, current rates.
And you get the same output.
So that's the final codes.
Now later in the course, we will build a desktop application with a graphical user interface where
the user can choose from.
A dropdown list can choose the currency rates, the source currency rate and the target currency rates.
Also in another dropdown list.
And then they can enter a value and then click a button and then get the converted rate in the target's
its currency in the user interface.
So it's a real desktop program.
We will build this ain't the gooey section where we built applications for the down in the course.
So that's something exciting that I wanted you to know.
And I'll talk to you later.
Thank you.