1
00:00:01,370 --> 00:00:03,590
In this video, we will build together

2
00:00:05,020 --> 00:00:09,790
the script that solves the exercise, the current exercise.

3
00:00:10,000 --> 00:00:16,930
So this script is going to scrape a value from this web page, this particular value every two seconds

4
00:00:17,290 --> 00:00:19,450
because this value is changing every two seconds.

5
00:00:19,780 --> 00:00:26,860
So we're going to get that value and store each value in a text file.

6
00:00:27,280 --> 00:00:29,440
So let's see how we can do that.

7
00:00:30,100 --> 00:00:34,210
This function gets the driver for the homepage, right?

8
00:00:35,080 --> 00:00:37,540
And we get that driver in here.

9
00:00:39,530 --> 00:00:44,600
We sleep for two seconds and then we get that element.

10
00:00:45,410 --> 00:00:51,020
So what we need to change here, I think, is that we need to give these elements multiple times.

11
00:00:51,530 --> 00:00:59,450
Therefore, it makes sense to put that element inside a loop, this scraping here.

12
00:01:00,020 --> 00:01:05,690
So I'm going to select that and also time the Sleep Press tab to indent them.

13
00:01:06,080 --> 00:01:13,160
And then while true, run these every two seconds.

14
00:01:13,730 --> 00:01:20,150
So the loop will first wait two seconds after we load the page, so we load the page.

15
00:01:20,660 --> 00:01:22,200
The loop starts immediately.

16
00:01:22,220 --> 00:01:26,330
It waits two seconds and then it gets the element, in here.

17
00:01:27,650 --> 00:01:30,410
So what do we want to do with the element?

18
00:01:31,700 --> 00:01:37,670
Well, let's start by extracting the texts, so I'm going to delete this return statement.

19
00:01:37,880 --> 00:01:43,160
So, we want to extract the text, which is equal to that, and delete return.

20
00:01:43,730 --> 00:01:51,920
We will probably not need to return any value here since we are not printing out things in the terminal,

21
00:01:51,920 --> 00:01:59,060
but we are writing things in a file that means our function will not contain any return value.

22
00:01:59,660 --> 00:02:04,910
So once we get the text, we want to write that text to a file.

23
00:02:05,420 --> 00:02:15,080
So should we do with here or does it make more sense to create a function that is responsible for creating

24
00:02:15,080 --> 00:02:23,420
files such as define write_file, which gets texts just a parameter as input, and then let's

25
00:02:23,420 --> 00:02:24,920
you write a doc string for this?

26
00:02:25,930 --> 00:02:36,910
Such as "Write input text into a text file" with double quotes, and so let's define what file name

27
00:02:36,910 --> 00:02:38,530
will this text contain

28
00:02:39,070 --> 00:02:44,770
before we do with open file name, 'w'

29
00:02:44,770 --> 00:02:48,220
as file file.write.

30
00:02:50,300 --> 00:02:56,060
Something here, which is the text, the content, so text will be written inside the file with this

31
00:02:56,060 --> 00:02:56,480
file name.

32
00:02:56,600 --> 00:03:02,150
So the file name is important here because we don't want to have duplicate file names. To avoid having

33
00:03:02,150 --> 00:03:03,230
duplicate file names,

34
00:03:03,860 --> 00:03:11,600
we need to use something like a date time that changes every two seconds that will make our date times

35
00:03:11,600 --> 00:03:12,110
unique.

36
00:03:12,710 --> 00:03:14,300
So file name would be equal to.

37
00:03:16,300 --> 00:03:17,450
And f string.

38
00:03:18,130 --> 00:03:22,120
So which is going to be something like .txt?

39
00:03:22,480 --> 00:03:27,950
And here we have a placeholder, which will be the dt.now.

40
00:03:27,970 --> 00:03:28,940
So date time.

41
00:03:29,410 --> 00:03:30,280
What is dt?

42
00:03:30,310 --> 00:03:39,520
Well, dt is something we need to import from datetime, a standard Python library import datetime

43
00:03:39,730 --> 00:03:45,220
as the dt so dt will be the variable equivalent to this datetime.

44
00:03:46,600 --> 00:03:50,650
The object that returns datetime instances.

45
00:03:52,140 --> 00:03:58,110
So dt.now gives us the current datetime, as you may know.

46
00:03:58,350 --> 00:04:04,950
So just if I copy that, paste it here and say dt.now gives us that.

47
00:04:05,430 --> 00:04:15,300
And from that, if we apply str f time, which stands for string from time, so we want to generate

48
00:04:15,600 --> 00:04:18,060
a string out of that datetime object.

49
00:04:18,420 --> 00:04:20,550
This is a datetime object, right?

50
00:04:21,120 --> 00:04:26,340
We need a string because you use that string as a file name.

51
00:04:27,090 --> 00:04:35,730
So this file name here that we provide to the open function is expected to be a string, so text.

52
00:04:36,480 --> 00:04:45,570
Therefore, we need to convert D.T. now to text because currently the type of data that now is a date

53
00:04:45,570 --> 00:04:47,730
time, class type.

54
00:04:48,210 --> 00:04:58,650
So again, dt.now().strftime and inside here you want to provide some data time flags or

55
00:04:58,650 --> 00:05:02,820
directives or format code or whatever we would like to call them.

56
00:05:03,030 --> 00:05:06,900
You can find these easily on the web if you search for datetime format.

57
00:05:07,370 --> 00:05:07,920
Python.

58
00:05:08,290 --> 00:05:12,060
So we want to extract basically, we said the year.

59
00:05:12,990 --> 00:05:15,540
So we say y. If we execute this...

60
00:05:15,990 --> 00:05:18,180
Sorry, it's a capital Y.

61
00:05:18,630 --> 00:05:21,200
If you execute that's you get the current year.

62
00:05:22,050 --> 00:05:26,940
Now, if you add a dash, you get the current year and the dash.

63
00:05:27,900 --> 00:05:34,860
If you add after the dash, a small m, you get the current year and the number of the month and so

64
00:05:34,860 --> 00:05:40,410
on, keep adding things like that and you will construct the date time as a string.

65
00:05:41,010 --> 00:05:43,340
So next would be the day, right?

66
00:05:43,650 --> 00:05:55,950
And then maybe have a dot and then have the hour, the minutes with capital M Dash and seconds with capital

67
00:05:55,950 --> 00:05:56,370
S.

68
00:05:57,030 --> 00:05:57,900
Yeah, that's it.

69
00:05:59,310 --> 00:06:00,690
And we get this string.

70
00:06:01,020 --> 00:06:06,120
So any time I execute this, we're going to get the string different from the first one.

71
00:06:06,420 --> 00:06:09,410
So four seconds later, right?

72
00:06:10,200 --> 00:06:10,620
Six.

73
00:06:11,590 --> 00:06:13,990
Two seconds later and so on.

74
00:06:14,980 --> 00:06:15,190
Right.

75
00:06:15,370 --> 00:06:22,720
I'm going to copy that expression and paste it inside these curly brackets entirely.

76
00:06:23,500 --> 00:06:30,610
So that's string dot txt constructs the file name, and then we create a file with that file name in

77
00:06:30,610 --> 00:06:31,030
write mode.

78
00:06:31,030 --> 00:06:38,920
The file object is stored in this variable and in that file object we write the text.

79
00:06:41,580 --> 00:06:49,680
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.

80
00:06:49,710 --> 00:06:51,030
Remember it's a float.

81
00:06:51,330 --> 00:07:01,650
Therefore, I will convert this into a string so that we can provide that text to write file, so write

82
00:07:01,650 --> 00:07:02,640
file text.

83
00:07:04,020 --> 00:07:07,700
So this variable goes here, now you could say, why 

84
00:07:07,710 --> 00:07:14,200
don't you just delete this float and it will be a string anyway, so you don't have to convert first

85
00:07:14,200 --> 00:07:16,440
to a float and then you convert it back to a string?

86
00:07:16,440 --> 00:07:18,270
But I wouldn't prefer that method.

87
00:07:18,270 --> 00:07:27,300
I would like to leave this function independent, so I don't want to create coupling between the functions.

88
00:07:27,810 --> 00:07:33,300
The role of this function is to clean the text and to give that value as a number as flow.

89
00:07:33,300 --> 00:07:36,440
So don't mess with that function, just convert it into a string.

90
00:07:36,450 --> 00:07:37,050
No problem.

91
00:07:37,350 --> 00:07:39,510
It doesn't take any resources

92
00:07:40,260 --> 00:07:45,090
practically. This looks good to me, so I'll give it a try.

93
00:07:45,690 --> 00:07:46,320
So run.

94
00:07:53,690 --> 00:07:54,800
This is lagging.

95
00:07:55,100 --> 00:07:59,990
So I will try to reload, and run again, it sometimes happens to repl.

96
00:08:01,590 --> 00:08:05,310
But you will not lose any changes that you made to your script.

97
00:08:05,910 --> 00:08:06,660
So run again.

98
00:08:13,920 --> 00:08:15,570
And we have a syntax error here.

99
00:08:15,930 --> 00:08:22,020
So as you see, repl is also underlining this because we have double quotes inside double quotes.

100
00:08:22,320 --> 00:08:25,530
The outer double quotes is this and that one.

101
00:08:25,740 --> 00:08:29,160
And this should be single quotes here.

102
00:08:30,210 --> 00:08:33,280
Single quotes in here, run again.

103
00:08:35,309 --> 00:08:37,380
The browser is starting.

104
00:08:45,580 --> 00:08:50,020
And here is our first text file, so second 32, 

105
00:08:50,110 --> 00:08:52,780
34, 36, 38.

106
00:08:52,960 --> 00:08:53,680
Let's open them.

107
00:08:54,100 --> 00:08:54,820
34.

108
00:08:54,850 --> 00:08:56,310
25.0.

109
00:08:56,320 --> 00:08:59,380
The current world temperature, the fake temperature.

110
00:08:59,680 --> 00:09:03,700
28 and so on and so forth.

111
00:09:04,090 --> 00:09:07,720
Yep, it's working well like that.

112
00:09:08,230 --> 00:09:11,950
Sometimes, though, you will have some delays.

113
00:09:11,950 --> 00:09:17,050
For example, from 50, it jumped up to 53.

114
00:09:17,080 --> 00:09:23,290
There will be a mismatch between seconds, but I wouldn't worry about that.

115
00:09:24,010 --> 00:09:28,570
Still, we have one file generated every two seconds, so.

116
00:09:31,470 --> 00:09:32,340
That's how you do it.

117
00:09:33,270 --> 00:09:37,890
Thanks a lot, and I'll talk to you in the next videos.

