1 00:00:05,150 --> 00:00:09,230 Customizing requests with your library. 2 00:00:09,740 --> 00:00:10,610 Hello, everyone. 3 00:00:10,850 --> 00:00:18,530 So far, we learned how to obtain headers of certain files, but why we actually need them and what 4 00:00:18,530 --> 00:00:19,460 is their use? 5 00:00:20,000 --> 00:00:26,840 Well, usually it is a very good practice to add headers to the request even before sending it. 6 00:00:27,970 --> 00:00:36,100 So if I create a new here to try to do this, we basically need to follow three steps in. 7 00:00:36,100 --> 00:00:49,780 The first step will be create request object and this is usually done by the request. 8 00:00:51,930 --> 00:01:03,990 Class and we actually already use that class, then we would like to add headers to the request object 9 00:01:04,050 --> 00:01:06,090 that we create from the request class. 10 00:01:06,360 --> 00:01:06,880 OK. 11 00:01:07,530 --> 00:01:11,540 And after we create the request, object puts a physical thing. 12 00:01:11,850 --> 00:01:18,390 So from the blueprint of objects of requests, sorry, you create object call to request. 13 00:01:18,750 --> 00:01:22,820 So after you created it, you would like to open it. 14 00:01:22,830 --> 00:01:24,720 So we would use. 15 00:01:26,730 --> 00:01:29,490 And often feature. 16 00:01:33,650 --> 00:01:46,190 In fighting to open the request object and actually this feature you're also familiar with, and this 17 00:01:46,190 --> 00:01:49,760 is the Eurorail open feature. 18 00:01:50,840 --> 00:01:55,820 OK, you can use this from the Python packages. 19 00:01:56,480 --> 00:01:58,250 So let me show you how this is done. 20 00:01:59,180 --> 00:02:06,560 If I go to the terminal and now we're in python mode, let's first import some Labruzzo right from Eurorail. 21 00:02:08,650 --> 00:02:13,930 Library dot request import. 22 00:02:16,230 --> 00:02:24,720 Request Colma Eurorail open so you can import both of them at the same time and then following step 23 00:02:24,720 --> 00:02:34,110 two here sort of step one after we imported it, we need to create an object from the request glass. 24 00:02:34,590 --> 00:02:35,640 That's very simple. 25 00:02:35,650 --> 00:02:37,830 Let's write the request equals. 26 00:02:39,690 --> 00:02:40,570 Request. 27 00:02:40,860 --> 00:02:47,310 So now we're creating an object from the request clause and the. 28 00:02:48,650 --> 00:02:53,390 All project is going to be referred to the HTP 29 00:02:55,360 --> 00:02:57,300 YouTube dot com. 30 00:02:57,950 --> 00:02:58,540 OK. 31 00:03:01,030 --> 00:03:09,520 That them and then if you'd like to other hadrosaur, for example, now the request is created, but 32 00:03:09,520 --> 00:03:14,760 it's a good idea as we head toward the header to the request and this is the second step. 33 00:03:14,770 --> 00:03:15,950 And how do we have here? 34 00:03:16,090 --> 00:03:16,840 Very simple. 35 00:03:17,290 --> 00:03:23,500 Let's try to request the dot out here and here. 36 00:03:23,500 --> 00:03:26,030 You can basically tab the header that you would like to add. 37 00:03:26,050 --> 00:03:33,760 For example, I can see a separate language actually do a dash here. 38 00:03:35,770 --> 00:03:37,000 A language. 39 00:03:39,360 --> 00:03:39,920 OK. 40 00:03:46,360 --> 00:03:46,990 Cómo. 41 00:03:49,910 --> 00:03:59,240 And now or let's do em for English, OK, let's hit enter here and for example. 42 00:04:01,810 --> 00:04:06,440 Let's now open our request and see the hear of the request. 43 00:04:06,790 --> 00:04:14,050 So obviously when you want to check out a request, you sometimes will see all the information because 44 00:04:14,050 --> 00:04:15,040 there is plenty there. 45 00:04:15,310 --> 00:04:21,700 But maybe you would just like to see some information that will do what the data there is all about. 46 00:04:21,700 --> 00:04:29,700 So you can simply do a response because you're so open. 47 00:04:30,130 --> 00:04:32,310 And then I will do request. 48 00:04:32,320 --> 00:04:38,560 I worked in the records and then we can actually read the lines from that, the request that we already 49 00:04:38,560 --> 00:04:38,840 made. 50 00:04:39,130 --> 00:04:45,430 So here we already open the request, which is a third step with the euro open. 51 00:04:45,640 --> 00:04:50,260 And now let's check actually the result of our work. 52 00:04:50,590 --> 00:05:02,040 So if I do response that read lines and then I would like to check, let's say the fifth line. 53 00:05:02,140 --> 00:05:04,980 So fifth, OK. 54 00:05:05,440 --> 00:05:08,560 And you can see how massive for example, the fifth line it is. 55 00:05:08,740 --> 00:05:15,640 I mean, I see it missed here, but if you have a software to open this line in a structured way, it 56 00:05:15,640 --> 00:05:16,610 won't look that messy. 57 00:05:16,870 --> 00:05:24,910 So if I would like to check to see the header items, I can do Rikyu dot ared sorry. 58 00:05:25,360 --> 00:05:30,810 Raicu dot header items. 59 00:05:31,270 --> 00:05:31,760 OK. 60 00:05:33,220 --> 00:05:38,410 And if I hit enter, you can see that the code here is the YouTube dot com. 61 00:05:38,950 --> 00:05:42,640 The user agent is Python three point nine. 62 00:05:42,910 --> 00:05:48,530 And then you also get the addition to the header, that violent language m. 63 00:05:49,190 --> 00:05:52,780 OK, so this is how you can actually act. 64 00:05:52,780 --> 00:05:59,230 Information to the header is quite important, especially if you want to add something for the readers 65 00:05:59,230 --> 00:06:01,670 or for the people that are checking your request. 66 00:06:02,080 --> 00:06:10,190 So as I said, you can simply use these three steps, creating the request from the request class and 67 00:06:10,210 --> 00:06:15,030 then adding the header, then sending the request by the euro command. 68 00:06:15,670 --> 00:06:22,420 And finally, you can simply check the header by this comma and you can see that the additional here 69 00:06:22,420 --> 00:06:24,010 that you added is already there. 70 00:06:24,670 --> 00:06:28,590 So let's see how this will work if we added in the file. 71 00:06:29,050 --> 00:06:35,710 So let's create a new python file called add header. 72 00:06:37,510 --> 00:06:45,820 OK, so let me get a bit closer and open this up a little bit so we can see actually better. 73 00:06:47,040 --> 00:06:48,520 OK, I think that's better. 74 00:06:49,030 --> 00:06:50,500 So let's create the file. 75 00:06:50,530 --> 00:06:57,860 Now let's do a hashtag user being EMV. 76 00:06:58,510 --> 00:07:02,800 So this is where Python is vital three. 77 00:07:04,210 --> 00:07:20,620 OK, and then we can do from bureau library not to request import requests and then we can do user agent. 78 00:07:21,520 --> 00:07:27,700 OK, this is a custom variable and we can set the user agent as. 79 00:07:30,370 --> 00:07:31,240 Mosiuoa. 80 00:07:33,190 --> 00:07:39,640 5.0, this is the version, then Windows. 81 00:07:41,700 --> 00:07:55,410 Ninety five point one same McAloon RV, and then we can do to any point zero that said, the lowest 82 00:07:55,410 --> 00:08:04,050 rate, the G, C, K, all twenty, ten to zero for one or one. 83 00:08:05,940 --> 00:08:12,570 And this is simply an example, user agent and then Firefox. 84 00:08:14,420 --> 00:08:15,350 Slashed 20. 85 00:08:16,470 --> 00:08:25,170 OK, and then the URL, so I'll write you your URL will be equal to H TTP. 86 00:08:27,730 --> 00:08:34,700 YouTube and dot com, our favorite browser, and after that, I will write a function. 87 00:08:35,200 --> 00:08:37,560 So let's try find out. 88 00:08:39,740 --> 00:08:42,810 Heather, and this will be our main function. 89 00:08:43,400 --> 00:08:46,200 Let me underscore here, that's it. 90 00:08:47,040 --> 00:08:49,600 OK, so let's create the variable. 91 00:08:49,910 --> 00:08:56,600 This will be called Heather's Equals Said. 92 00:08:59,620 --> 00:09:00,460 Language. 93 00:09:02,720 --> 00:09:04,130 In the same column. 94 00:09:06,730 --> 00:09:07,570 And one. 95 00:09:09,300 --> 00:09:13,230 Comma, and then I will do user. 96 00:09:15,540 --> 00:09:22,800 Agents in the same column, user agent that already created. 97 00:09:23,760 --> 00:09:30,540 OK, so actually, the user agent is going to be just one of the predefined variable that have already 98 00:09:30,540 --> 00:09:31,570 created up here. 99 00:09:32,630 --> 00:09:33,980 Let's go to the next one. 100 00:09:34,320 --> 00:09:36,690 So I to request. 101 00:09:38,410 --> 00:09:44,640 Equals to request and then Eurail. 102 00:09:45,910 --> 00:09:49,610 OK, so this is the area, that group predefined YouTube dot com. 103 00:09:49,800 --> 00:09:55,290 Actually here I missed the slushies, but that's fine now. 104 00:09:55,630 --> 00:09:57,220 So let's go after that. 105 00:09:58,180 --> 00:10:00,700 Heather's equals. 106 00:10:02,990 --> 00:10:16,040 Heather's OK, and then this right to print and in the print statement, I will write request headers. 107 00:10:18,020 --> 00:10:32,240 OK, and then for Key to follow in the request to adopt Heather. 108 00:10:34,700 --> 00:10:35,600 Items. 109 00:10:35,910 --> 00:10:36,430 OK. 110 00:10:39,160 --> 00:10:41,860 So for this, I will print. 111 00:10:45,570 --> 00:10:50,670 And here we're going to print percent is. 112 00:10:52,580 --> 00:10:59,610 That percent is then percent key value. 113 00:11:00,890 --> 00:11:02,810 Let me right, come on here. 114 00:11:04,250 --> 00:11:13,640 You follow, and that's it, actually labor removed and I wouldn't put it exactly here. 115 00:11:15,260 --> 00:11:15,910 That's it. 116 00:11:15,940 --> 00:11:23,650 So first of all, the key and then the volume, OK, and now let's run the function by simply typing 117 00:11:23,650 --> 00:11:25,960 if name. 118 00:11:32,880 --> 00:11:36,240 Equals Main. 119 00:11:39,390 --> 00:11:46,830 Look, month underscores the first name, of course, main, then I would add. 120 00:11:49,350 --> 00:11:56,880 Heather, OK, to run the function, this how best to run the functions in Python, so let's save this 121 00:11:56,880 --> 00:11:59,520 file now and let's try to run it from the terminal. 122 00:12:00,060 --> 00:12:02,190 So how is it the python mode? 123 00:12:03,360 --> 00:12:04,610 And let's try. 124 00:12:04,690 --> 00:12:06,710 It was here to see what we've got. 125 00:12:07,050 --> 00:12:12,840 So let's write Python Python up, Heather. 126 00:12:15,070 --> 00:12:25,750 And you can see that after a press of Heather, we automatically get the same language as anyone as 127 00:12:25,750 --> 00:12:26,470 we set it up. 128 00:12:27,190 --> 00:12:32,740 OK, the user agent, we get the user agent to set it up on the top. 129 00:12:33,070 --> 00:12:38,030 And so this was the information that we requested in here in the header section. 130 00:12:38,350 --> 00:12:43,600 So our test is completely successful in the final run properly. 131 00:12:44,980 --> 00:12:51,040 So this is how, guys, you can pretty much design and art headers to your euros. 132 00:12:51,070 --> 00:12:54,510 I hope this video was useful and I see you in the next one.