In this video, we will build together
the script that solves the exercise, the current exercise.
So this script is going to scrape a value from this web page, this particular value every two seconds
because this value is changing every two seconds.
So we're going to get that value and store each value in a text file.
So let's see how we can do that.
This function gets the driver for the homepage, right?
And we get that driver in here.
We sleep for two seconds and then we get that element.
So what we need to change here, I think, is that we need to give these elements multiple times.
Therefore, it makes sense to put that element inside a loop, this scraping here.
So I'm going to select that and also time the Sleep Press tab to indent them.
And then while true, run these every two seconds.
So the loop will first wait two seconds after we load the page, so we load the page.
The loop starts immediately.
It waits two seconds and then it gets the element, in here.
So what do we want to do with the element?
Well, let's start by extracting the texts, so I'm going to delete this return statement.
So, we want to extract the text, which is equal to that, and delete return.
We will probably not need to return any value here since we are not printing out things in the terminal,
but we are writing things in a file that means our function will not contain any return value.
So once we get the text, we want to write that text to a file.
So should we do with here or does it make more sense to create a function that is responsible for creating
files such as define write_file, which gets texts just a parameter as input, and then let's
you write a doc string for this?
Such as "Write input text into a text file" with double quotes, and so let's define what file name
will this text contain
before we do with open file name, 'w'
as file file.write.
Something here, which is the text, the content, so text will be written inside the file with this
file name.
So the file name is important here because we don't want to have duplicate file names. To avoid having
duplicate file names,
we need to use something like a date time that changes every two seconds that will make our date times
unique.
So file name would be equal to.
And f string.
So which is going to be something like .txt?
And here we have a placeholder, which will be the dt.now.
So date time.
What is dt?
Well, dt is something we need to import from datetime, a standard Python library import datetime
as the dt so dt will be the variable equivalent to this datetime.
The object that returns datetime instances.
So dt.now gives us the current datetime, as you may know.
So just if I copy that, paste it here and say dt.now gives us that.
And from that, if we apply str f time, which stands for string from time, so we want to generate
a string out of that datetime object.
This is a datetime object, right?
We need a string because you use that string as a file name.
So this file name here that we provide to the open function is expected to be a string, so text.
Therefore, we need to convert D.T. now to text because currently the type of data that now is a date
time, class type.
So again, dt.now().strftime and inside here you want to provide some data time flags or
directives or format code or whatever we would like to call them.
You can find these easily on the web if you search for datetime format.
Python.
So we want to extract basically, we said the year.
So we say y. If we execute this...
Sorry, it's a capital Y.
If you execute that's you get the current year.
Now, if you add a dash, you get the current year and the dash.
If you add after the dash, a small m, you get the current year and the number of the month and so
on, keep adding things like that and you will construct the date time as a string.
So next would be the day, right?
And then maybe have a dot and then have the hour, the minutes with capital M Dash and seconds with capital
S.
Yeah, that's it.
And we get this string.
So any time I execute this, we're going to get the string different from the first one.
So four seconds later, right?
Six.
Two seconds later and so on.
Right.
I'm going to copy that expression and paste it inside these curly brackets entirely.
So that's string dot txt constructs the file name, and then we create a file with that file name in
write mode.
The file object is stored in this variable and in that file object we write the text.
So now what we want to do is we want to get the text here and clean it, and this will give us a float.
Remember it's a float.
Therefore, I will convert this into a string so that we can provide that text to write file, so write
file text.
So this variable goes here, now you could say, why
don't you just delete this float and it will be a string anyway, so you don't have to convert first
to a float and then you convert it back to a string?
But I wouldn't prefer that method.
I would like to leave this function independent, so I don't want to create coupling between the functions.
The role of this function is to clean the text and to give that value as a number as flow.
So don't mess with that function, just convert it into a string.
No problem.
It doesn't take any resources
practically. This looks good to me, so I'll give it a try.
So run.
This is lagging.
So I will try to reload, and run again, it sometimes happens to repl.
But you will not lose any changes that you made to your script.
So run again.
And we have a syntax error here.
So as you see, repl is also underlining this because we have double quotes inside double quotes.
The outer double quotes is this and that one.
And this should be single quotes here.
Single quotes in here, run again.
The browser is starting.
And here is our first text file, so second 32,
34, 36, 38.
Let's open them.
34.
25.0.
The current world temperature, the fake temperature.
28 and so on and so forth.
Yep, it's working well like that.
Sometimes, though, you will have some delays.
For example, from 50, it jumped up to 53.
There will be a mismatch between seconds, but I wouldn't worry about that.
Still, we have one file generated every two seconds, so.
That's how you do it.
Thanks a lot, and I'll talk to you in the next videos.