1 00:00:04,940 --> 00:00:13,250 Using requests to handle cookies, hello, everyone, since the cookies topic is quite interesting because 2 00:00:13,250 --> 00:00:18,210 everybody's interested about how his data is exchanged over the network. 3 00:00:18,380 --> 00:00:25,240 I decided to make another video which will show you how you can hear the hand of cookies using requests. 4 00:00:25,610 --> 00:00:29,960 So let's create a request to obtain some cookies. 5 00:00:29,960 --> 00:00:32,600 So I will do a response. 6 00:00:33,050 --> 00:00:37,490 Equal extra let's and Python more to write Python. 7 00:00:38,270 --> 00:00:39,320 So let's right. 8 00:00:40,280 --> 00:00:47,750 Let's first actually import your library and library 9 00:00:50,450 --> 00:00:58,620 request and also import htp dot. 10 00:01:00,560 --> 00:01:05,390 Cookie jar and let's right import. 11 00:01:07,800 --> 00:01:08,850 The requests. 12 00:01:11,310 --> 00:01:18,780 And then session equals requests that session. 13 00:01:20,400 --> 00:01:23,100 OK, so here we are creating a second class. 14 00:01:25,660 --> 00:01:31,630 In order to observe cookies from a website, but from which website would like to observe cookies, 15 00:01:31,870 --> 00:01:33,670 so let's write the response. 16 00:01:36,390 --> 00:01:42,630 Response, of course, request not to get. 17 00:01:46,040 --> 00:01:50,660 HTP, GitHub, dot com. 18 00:01:53,580 --> 00:01:57,420 That this request is not defined. 19 00:02:00,810 --> 00:02:07,450 Yes, so let's do them your old Labrador's request so that I will two requests. 20 00:02:08,400 --> 00:02:10,550 Yes, I think that's going to be a sentence. 21 00:02:11,190 --> 00:02:11,790 That's it. 22 00:02:12,300 --> 00:02:13,950 It took some time, but now it's good. 23 00:02:14,280 --> 00:02:22,500 So let's run now print and then our response dog case. 24 00:02:24,360 --> 00:02:29,880 And you can see that now we're able to print the cookies from GitHub dot com. 25 00:02:29,880 --> 00:02:36,690 And you're already familiar with the cookies that we have from there, such as the logged in, which 26 00:02:36,690 --> 00:02:42,780 is now said to know and the rest of the cookies, like G.H. says and so on. 27 00:02:43,530 --> 00:02:45,180 So let's go back to the station. 28 00:02:45,480 --> 00:02:48,000 So let's print session. 29 00:02:48,480 --> 00:02:55,130 Not cookies, dot, get a dictionary, OK? 30 00:02:55,530 --> 00:02:58,020 And then let's do a response. 31 00:02:59,570 --> 00:03:00,410 Equals. 32 00:03:01,970 --> 00:03:15,140 Session not get, and now you can see the first example, we can't print any type of cookies simply 33 00:03:15,140 --> 00:03:17,630 because we don't pass any two main political parties. 34 00:03:17,960 --> 00:03:24,950 But now let's right here in the briskets, HTP, GitHub dot com. 35 00:03:25,730 --> 00:03:33,800 OK, and now let's do print's then session dot Corky's. 36 00:03:34,580 --> 00:03:40,030 Don't get underscore dictionary, OK? 37 00:03:40,880 --> 00:03:42,560 And I think that should be fine. 38 00:03:43,610 --> 00:03:50,450 So you can see that this is another way that we can actually print all the cookies which some alternative 39 00:03:50,450 --> 00:03:51,310 to the first one. 40 00:03:52,040 --> 00:03:59,570 So you can either do it with the get method or with them get a dictionary from the session. 41 00:03:59,960 --> 00:04:00,370 OK. 42 00:04:01,340 --> 00:04:10,390 And so here we are using the same method in the same way as we used it in the requests I don't get. 43 00:04:10,730 --> 00:04:16,570 But at this time we're using the class section in order to receive the cookies from the website. 44 00:04:16,910 --> 00:04:26,330 So let's go now back to the ID, to the pie chart and let's create a new file in the file will be called 45 00:04:26,330 --> 00:04:27,730 to Get Cookies. 46 00:04:27,980 --> 00:04:31,660 So let's create the new Python file, actually. 47 00:04:32,090 --> 00:04:39,230 And this file will be called Get Cookies, OK, and let's write it. 48 00:04:39,260 --> 00:04:40,550 So now let's do it. 49 00:04:41,420 --> 00:04:45,440 So this file is basically going to extract the cookies in there. 50 00:04:45,440 --> 00:04:50,640 And there's a graphical user interface from GitHub, dot com or from any other domain. 51 00:04:51,080 --> 00:04:56,690 So what I'm going to do is first I will do the Python directory. 52 00:04:56,810 --> 00:04:58,010 So user. 53 00:04:59,790 --> 00:05:12,600 We've been EMV Python three, that's it, and then there's two import requests let's remove this morning 54 00:05:12,600 --> 00:05:13,010 here. 55 00:05:13,770 --> 00:05:16,140 So syntax, that's it. 56 00:05:16,650 --> 00:05:18,320 And let's try to function. 57 00:05:18,360 --> 00:05:23,090 So Jeff, check HTP only. 58 00:05:24,150 --> 00:05:28,490 OK, see, and that's right. 59 00:05:28,500 --> 00:05:36,210 If H htp only. 60 00:05:38,280 --> 00:05:39,420 In C. 61 00:05:41,560 --> 00:05:43,210 Does underscore. 62 00:05:44,810 --> 00:05:45,470 Rest. 63 00:05:47,480 --> 00:05:52,040 Not Kei's return, true. 64 00:05:54,160 --> 00:06:06,840 And then let's right else a return and in our case, ask to return this, call them cookies equal to 65 00:06:06,850 --> 00:06:07,290 a list. 66 00:06:07,300 --> 00:06:14,050 So we're creating a list from cookies and then I will do Eurail equal to. 67 00:06:16,200 --> 00:06:20,580 H t t p h h GTP. 68 00:06:22,740 --> 00:06:24,510 And give hope, dot com. 69 00:06:26,430 --> 00:06:29,130 OK, and in response. 70 00:06:31,470 --> 00:06:40,890 Response will be equal to requests don't get in the brickies euro. 71 00:06:42,330 --> 00:06:49,980 OK, so here we're getting the worst ineke requests to the dot com and we're getting the response. 72 00:06:50,370 --> 00:06:59,310 And now let's right for cocaine in response, not Corky's. 73 00:07:01,740 --> 00:07:14,310 Let's print first name, comma, cookie, top name, and then let's print. 74 00:07:17,280 --> 00:07:18,320 The value. 75 00:07:21,880 --> 00:07:27,440 Como quoque, Movado, OK? 76 00:07:28,270 --> 00:07:33,370 And finally, free cookies, cookies, dot. 77 00:07:37,420 --> 00:07:49,640 Who keep that value and then, if not kookier, not secure. 78 00:07:50,110 --> 00:07:59,590 So if the cookie is not secure or it doesn't have the secure code, who would like to cookie not secure 79 00:08:00,560 --> 00:08:01,450 you cause. 80 00:08:03,610 --> 00:08:19,930 Force and then out of Prince HTP, only coma, and then you can check HTP only and we'll check just 81 00:08:19,930 --> 00:08:26,290 for Cauchy came because we're still in the for loop, so forever kookier. 82 00:08:26,300 --> 00:08:35,320 We're going to check if it is HTP only and analyst right here and actually backslash. 83 00:08:35,320 --> 00:08:40,360 And so we go to a new role and that's to the base. 84 00:08:40,880 --> 00:08:46,720 Let's save this cold and let's go now to the terminal where we can run it. 85 00:08:46,730 --> 00:08:54,670 So I'll exit the python mode and I'll write aways so you can see that the file get actually I wrote 86 00:08:54,670 --> 00:09:00,250 and got to kiss me because I'm filming for already about one hour and a half. 87 00:09:01,450 --> 00:09:05,680 But let's actually run the file and see if everything is correct. 88 00:09:06,580 --> 00:09:12,600 So Python got cookies and we have some error here. 89 00:09:12,610 --> 00:09:15,610 Let's go back to the code and try to fix it. 90 00:09:16,090 --> 00:09:17,860 So here is the old guys. 91 00:09:18,880 --> 00:09:21,250 Here's cookies instead of Cookie. 92 00:09:22,270 --> 00:09:26,050 And this is why we've got this cookie system defined. 93 00:09:26,050 --> 00:09:29,260 The cost was actually not defined, but now it will be fine. 94 00:09:29,590 --> 00:09:32,650 Let's save it and I will run the code again. 95 00:09:33,220 --> 00:09:38,400 So now you can see that we're getting all the information that were requested. 96 00:09:39,670 --> 00:09:47,710 So for this cookie, we get the name we just logged in in a very nice graphics so you can easily distinguish 97 00:09:47,710 --> 00:09:47,890 it. 98 00:09:48,310 --> 00:09:57,690 Then you get the cookie volume, which is now and then you get if it is HTP only, which is here, false. 99 00:09:58,120 --> 00:09:59,320 So you can see that here. 100 00:09:59,500 --> 00:10:08,650 And after that you can see the actual cookie and again, the HTP only that is equal to force, which 101 00:10:08,650 --> 00:10:10,630 is exactly what you wrote in the code. 102 00:10:11,290 --> 00:10:16,420 So let's right here print set and cookies. 103 00:10:17,200 --> 00:10:23,050 So if I run the file again, you can see the difference that we see more than one cookie at this time. 104 00:10:23,590 --> 00:10:32,320 So here we see the set of all cookies and on the top we see the cookie with the information that we 105 00:10:32,320 --> 00:10:32,950 desired. 106 00:10:33,580 --> 00:10:36,360 So that said, guys, thank you very much for watching. 107 00:10:37,300 --> 00:10:40,180 This was actually quite an interesting lecture.