1
00:00:02,210 --> 00:00:06,890
In this lecture, you learn how to upload a file via Python.

2
00:00:07,240 --> 00:00:14,180
And for this, we will use as an example of this website, you can find the link in the resources of

3
00:00:14,180 --> 00:00:14,870
this lecture.

4
00:00:16,010 --> 00:00:18,650
Once you open the link, you'll see it down here.

5
00:00:18,910 --> 00:00:27,260
There's a form and we can upload the file through this form, either using the browser as I'm doing

6
00:00:27,260 --> 00:00:29,150
right now or by using Python.

7
00:00:29,660 --> 00:00:33,830
So first, press the Choose File Bottom and then find the file.

8
00:00:35,980 --> 00:00:39,370
And then press the press button to upload the file.

9
00:00:40,810 --> 00:00:46,540
And you should see this final webpage, which sees that you have uploaded file successfully.

10
00:00:46,810 --> 00:00:50,290
This is the contents of the file, so it was a text file in my case.

11
00:00:50,740 --> 00:00:56,620
That's why we can see the content since it was text, if it was an MP, three, for example, or something

12
00:00:56,620 --> 00:00:56,890
else.

13
00:00:57,550 --> 00:00:59,980
You will see maybe some bytes in here.

14
00:01:01,210 --> 00:01:09,400
So anyway, let's go back to the page where the upload form is and then go back to our Python environment

15
00:01:09,670 --> 00:01:12,880
and create a new script and import requests.

16
00:01:13,240 --> 00:01:19,720
So again, we will use requests just as we did previously when we downloaded the file.

17
00:01:19,990 --> 00:01:22,960
So previously we use the get methods.

18
00:01:23,170 --> 00:01:25,720
Now we will use the post methods.

19
00:01:26,140 --> 00:01:31,750
And of course, first we need a new URL pointing to the posts you won't know.

20
00:01:32,020 --> 00:01:33,520
What is the post URL?

21
00:01:33,820 --> 00:01:43,090
Well, you can find the post URL of a form by doing an uploads again, so just pick a file, then press

22
00:01:43,090 --> 00:01:43,960
the press button.

23
00:01:43,960 --> 00:01:48,910
And once you do that, this you are earlier in the browser will change to something else.

24
00:01:49,840 --> 00:01:52,360
So you see wait.

25
00:01:54,720 --> 00:02:00,150
So once you see the file, upload the results, then you see the URL has changed, you should copy that

26
00:02:00,150 --> 00:02:05,490
you URL, go to Python and put it inside quotes.

27
00:02:06,390 --> 00:02:11,370
So you see, in my case, this URL ends with CGI.

28
00:02:11,580 --> 00:02:19,540
Once you have that URL, then we're ready to make the request, which is the requests that post your

29
00:02:19,590 --> 00:02:21,250
URL is the first parameter.

30
00:02:21,270 --> 00:02:26,610
But we also need another parameter here, and that is the file we want to upload.

31
00:02:27,240 --> 00:02:32,960
So for this, I'm just going to create a new file and use it to upload it to that website.

32
00:02:33,000 --> 00:02:34,890
Just see my file that.

33
00:02:38,250 --> 00:02:40,500
Hey, I'm a new file.

34
00:02:42,830 --> 00:02:44,630
I'll get uploaded.

35
00:02:45,930 --> 00:02:52,280
That's a file we want to upload a simple text file, and we want to provide that file in here, but

36
00:02:52,610 --> 00:02:59,480
you don't want to provide the file path like that because this method expects the actual file object.

37
00:02:59,840 --> 00:03:09,170
So you need to create a file object using file is able to open, then you provide the path to the file

38
00:03:09,170 --> 00:03:11,360
you want to upload.

39
00:03:11,810 --> 00:03:16,970
So my file that takes the open, it's in our modes or B.

40
00:03:16,970 --> 00:03:22,370
It's even better because our B is both for binary and text files.

41
00:03:22,880 --> 00:03:28,550
In our case, it would also work if we just left it out or since this is about text files, but our

42
00:03:28,550 --> 00:03:38,810
B is more inclusive, so a file is the file object and then we provide a false argument, which is a

43
00:03:38,810 --> 00:03:39,570
dictionary.

44
00:03:40,250 --> 00:03:43,960
This dictionary has a key and a value.

45
00:03:43,970 --> 00:03:53,300
The value is the file object we have created in here, and the key is, well, the key of that comes

46
00:03:53,930 --> 00:03:55,670
from this form.

47
00:03:56,180 --> 00:03:59,510
So what you want to do is first to reload this page.

48
00:04:02,790 --> 00:04:11,160
Yes, then right click over the page and go to show page source or page souls or wherever.

49
00:04:12,500 --> 00:04:19,190
It is in your browser and then you want to scroll down to find the form tag you see down here.

50
00:04:19,670 --> 00:04:26,660
Otherwise, you can also use inspect elements, so click somewhere close to here and go to inspect elements.

51
00:04:27,170 --> 00:04:35,720
And again, you'll find that form tag and then expand it and you want to find the input type tag.

52
00:04:36,260 --> 00:04:42,830
This one in here and you want to see what value is attached to the name property.

53
00:04:43,040 --> 00:04:45,260
In my case, it is up file.

54
00:04:45,920 --> 00:04:49,310
So that is the place where we want to upload our file.

55
00:04:49,680 --> 00:04:55,160
Therefore, in here we should see up file in your form.

56
00:04:55,160 --> 00:04:59,030
It could be different, so make sure you have the correct value.

57
00:05:02,090 --> 00:05:07,790
So that's code we'll post that file to this Web page.

58
00:05:08,300 --> 00:05:11,750
Now all we are left to do is just print out.

59
00:05:16,450 --> 00:05:21,610
The results of the requests, so request the text and let's run it.

60
00:05:23,870 --> 00:05:28,010
I have a type of request like that we're on again.

61
00:05:30,780 --> 00:05:36,690
And that's the results, so basically what we get here is the same page that we get when we upload the

62
00:05:36,690 --> 00:05:38,040
file manually.

63
00:05:41,040 --> 00:05:42,360
By pressing the red button.

64
00:05:43,430 --> 00:05:48,320
So basically, we have this content displayed here.

65
00:05:48,440 --> 00:05:54,260
You see, file upload the results, file upload the results here as well.

66
00:05:55,580 --> 00:06:01,910
Here we have, of course, some estimate tags because this is a raw string to make sure that the file

67
00:06:01,910 --> 00:06:02,570
was uploaded.

68
00:06:02,900 --> 00:06:06,890
You should see here under the Files Contents Order.

69
00:06:07,400 --> 00:06:12,200
And then we have this tag and that tag, and then we have the contents of the file.

70
00:06:12,380 --> 00:06:13,460
Hey, I'm a new file.

71
00:06:13,460 --> 00:06:14,780
I will get uploaded.

72
00:06:15,140 --> 00:06:20,390
So that is the contents of the file we have uploaded, depending on the website.

73
00:06:20,540 --> 00:06:21,920
This could be different.

74
00:06:21,920 --> 00:06:27,620
So this web page shows us these results, but other web pages can show you something different.

75
00:06:28,640 --> 00:06:33,110
But generally, this is how you upload files via Python.

76
00:06:33,950 --> 00:06:37,610
So I hope that worked, and let's have some more videos suing the next one.

