Skip to content

Commit e1326d2

Browse files
committed
Add Requests notebook
1 parent e4edcd9 commit e1326d2

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ Python notebooks written while learning some libs
1010
[Matplotlib](https://www.youtube.com/watch?v=0P7QnIQDBJY&list=PLFCB5Dp81iNVmuoGIqcT5oF4K-7kTI5vp&index=5&ab_channel=KeithGalli)
1111

1212
[Plotly](https://plotly.com/python) - recreated the plots from matplotlib notebook using the doc.
13+
14+
[Requests](https://www.youtube.com/watch?v=tb8gHvYlCFs&ab_channel=CoreySchafer)

Requests.ipynb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.8.5-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "Python 3.8.5 32-bit",
18+
"display_name": "Python 3.8.5 32-bit",
19+
"metadata": {
20+
"interpreter": {
21+
"hash": "7b7c8faf1d9efd50dbba91a094ea215a99dec3f368cd8459cf0b9dd727c71e83"
22+
}
23+
}
24+
}
25+
},
26+
"nbformat": 4,
27+
"nbformat_minor": 2,
28+
"cells": [
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"output_type": "stream",
36+
"name": "stdout",
37+
"text": [
38+
"Python version: 3.8.5\n"
39+
]
40+
}
41+
],
42+
"source": [
43+
"import sys\n",
44+
"import requests as rs\n",
45+
"print(\"Python version:\", sys.version[:5])"
46+
]
47+
},
48+
{
49+
"source": [
50+
"# Learning Requests\n",
51+
"## Http get"
52+
],
53+
"cell_type": "markdown",
54+
"metadata": {}
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 20,
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"output_type": "stream",
63+
"name": "stdout",
64+
"text": [
65+
"<Response [200]>\n\n{'Connection': 'keep-alive', 'Content-Length': '3069', 'Server': 'nginx', 'Content-Type': 'text/html; charset=UTF-8', 'Last-Modified': 'Fri, 23 Oct 2020 04:00:04 GMT', 'ETag': 'W/\"5f925544-1dc6\"', 'Expires': 'Fri, 23 Oct 2020 16:14:28 GMT', 'Cache-Control': 'max-age=300', 'Content-Encoding': 'gzip', 'Accept-Ranges': 'bytes', 'Date': 'Fri, 23 Oct 2020 16:29:02 GMT', 'Via': '1.1 varnish', 'Age': '70', 'X-Served-By': 'cache-gru17144-GRU', 'X-Cache': 'HIT', 'X-Cache-Hits': '13', 'X-Timer': 'S1603470543.678268,VS0,VE0', 'Vary': 'Accept-Encoding'}\n\n200\n\nTrue\n\n<!DOCTYPE html>\n<html>\n<head>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"/s/7d94e0.css\" title=\"Default\"/>\n<title>xkcd: Python</title>\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"/>\n<link rel=\"shortcut icon\" href=\"/s/919f27.ico\" type=\"image/x-icon\"/>\n<link rel=\"icon\" href=\"/s/919f27.ico\" type=\"image/x-icon\"/>\n<link rel=\"alternate\" type=\"application/atom+xml\" title=\"Atom 1.0\" href=\"/atom.xml\"/>\n<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS 2.0\" href=\"/rss.xml\"/>\n<script type=\"text/javascript\" src=\"/s/b66ed7.js\" async></script>\n<script type=\"text/javascript\" src=\"/s/1b9456.js\" async></script>\n\n<meta property=\"og:site_name\" content=\"xkcd\">\n\n<meta property=\"og:title\" content=\"Python\">\n<meta property=\"og:url\" content=\"https://xkcd.com/353/\">\n<meta property=\"og:image\" content=\"https://imgs.xkcd.com/comics/\">\n<meta name=\"twitter:card\" content=\"summary_large_image\">\n\n</head>\n<body>\n<div id=\"topContainer\">\n<div id=\"topLeft\">\n<ul>\n<li><a href=\"/archive\">Archive</a></li>\n<li><a href=\"http://what-if.xkcd.com\">What If?</a></li>\n<li><a href=\"http://blag.xkcd.com\">Blag</a></li>\n<li><a href=\"/how-to/\">How To</a></li>\n<li><a href=\"http://store.xkcd.com/\">Store</a></li>\n<li><a rel=\"author\" href=\"/about\">About</a></li>\n<li><a href=\"/atom.xml\">Feed</a> &bull; <a href=\"/newsletter/\">Email</a></li>\n<li><a href=\"https://twitter.com/xkcd/\">TW</a> &bull; <a href=\"https://www.facebook.com/TheXKCD/\">FB</a> &bull; <a href=\"https://www.instagram.com/xkcd/\">IG</a></li>\n</ul>\n</div>\n<div id=\"topRight\">\n<div id=\"masthead\">\n<span><a href=\"/\"><img src=\"/s/0b7742.png\" alt=\"xkcd.com logo\" height=\"83\" width=\"185\"/></a></span>\n<span id=\"slogan\">A webcomic of romance,<br/> sarcasm, math, and language.</span>\n</div>\n<div id=\"news\">\n<div id=\"xkcdBanner\"><a href=\"https://blacklivesmatter.com/\"><img border=0 src=\"//imgs.xkcd.com/news/blm.png\"></a></div>\n<script>\nvar client = new XMLHttpRequest();\nclient.open(\"GET\", \"//c.xkcd.com/xkcd/news\", true);\nclient.send();\nclient.onreadystatechange = function() {\n if(client.readyState == 4 && client.status == 200) {\n document.getElementById(\"xkcdBanner\").innerHTML = client.responseText;\n }\n}\n</script>\n</div>\n</div>\n<div id=\"bgLeft\" class=\"bg box\"></div>\n<div id=\"bgRight\" class=\"bg box\"></div>\n</div>\n<div id=\"middleContainer\" class=\"box\">\n\n<div id=\"ctitle\">Python</div>\n<ul class=\"comicNav\">\n<li><a href=\"/1/\">|&lt;</a></li>\n<li><a rel=\"prev\" href=\"/352/\" accesskey=\"p\">&lt; Prev</a></li>\n<li><a href=\"//c.xkcd.com/random/comic/\">Random</a></li>\n<li><a rel=\"next\" href=\"/354/\" accesskey=\"n\">Next &gt;</a></li>\n<li><a href=\"/\">&gt;|</a></li>\n</ul>\n<div id=\"comic\">\n<img src=\"//imgs.xkcd.com/comics/python.png\" title=\"I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I&#39;m leaving you.\" alt=\"Python\" style=\"image-orientation:none\" />\n</div>\n<ul class=\"comicNav\">\n<li><a href=\"/1/\">|&lt;</a></li>\n<li><a rel=\"prev\" href=\"/352/\" accesskey=\"p\">&lt; Prev</a></li>\n<li><a href=\"//c.xkcd.com/random/comic/\">Random</a></li>\n<li><a rel=\"next\" href=\"/354/\" accesskey=\"n\">Next &gt;</a></li>\n<li><a href=\"/\">&gt;|</a></li>\n</ul>\n<br />\nPermanent link to this comic: https://xkcd.com/353/<br />\nImage URL (for hotlinking/embedding): https://imgs.xkcd.com/comics/python.png\n\n<div id=\"transcript\" style=\"display: none\">[[ Guy 1 is talking to Guy 2, who is floating in the sky ]]\nGuy 1: You&#39;re flying! How?\nGuy 2: Python!\nGuy 2: I learned it last night! Everything is so simple!\nGuy 2: Hello world is just &#39;print &quot;Hello, World!&quot; &#39;\nGuy 1: I dunno... Dynamic typing? Whitespace?\nGuy 2: Come join us! Programming is fun again! It&#39;s a whole new world up here!\nGuy 1: But how are you flying?\nGuy 2: I just typed &#39;import antigravity&#39;\nGuy 1: That&#39;s it?\nGuy 2: ...I also sampled everything in the medicine cabinet for comparison.\nGuy 2: But i think this is the python.\n{{ I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I&#39;m leaving you. }}</div>\n</div>\n<div id=\"bottom\" class=\"box\">\n<img src=\"//imgs.xkcd.com/s/a899e84.jpg\" width=\"520\" height=\"100\" alt=\"Selected Comics\" usemap=\"#comicmap\"/>\n<map id=\"comicmap\" name=\"comicmap\">\n<area shape=\"rect\" coords=\"0,0,100,100\" href=\"/150/\" alt=\"Grownups\"/>\n<area shape=\"rect\" coords=\"104,0,204,100\" href=\"/730/\" alt=\"Circuit Diagram\"/>\n<area shape=\"rect\" coords=\"208,0,308,100\" href=\"/162/\" alt=\"Angular Momentum\"/>\n<area shape=\"rect\" coords=\"312,0,412,100\" href=\"/688/\" alt=\"Self-Description\"/>\n<area shape=\"rect\" coords=\"416,0,520,100\" href=\"/556/\" alt=\"Alternative Energy Revolution\"/>\n</map>\n<br />\n<a href=\"//xkcd.com/1732/\"><img border=0 src=\"//imgs.xkcd.com/s/temperature.png\" width=\"520\" height=\"100\" alt=\"Earth temperature timeline\"></a>\n<br />\n<div>\n<!--\nSearch comic titles and transcripts:\n<script type=\"text/javascript\" src=\"//www.google.com/jsapi\"></script>\n<script type=\"text/javascript\">google.load('search', '1');google.setOnLoadCallback(function() {google.search.CustomSearchControl.attachAutoCompletion('012652707207066138651:zudjtuwe28q',document.getElementById('q'),'cse-search-box');});</script>\n<form action=\"//www.google.com/cse\" id=\"cse-search-box\">\n<div>\n<input type=\"hidden\" name=\"cx\" value=\"012652707207066138651:zudjtuwe28q\"/>\n<input type=\"hidden\" name=\"ie\" value=\"UTF-8\"/>\n<input type=\"text\" name=\"q\" id=\"q\" size=\"31\"/>\n<input type=\"submit\" name=\"sa\" value=\"Search\"/>\n</div>\n</form>\n<script type=\"text/javascript\" src=\"//www.google.com/cse/brand?form=cse-search-box&amp;lang=en\"></script>\n-->\n<a href=\"/rss.xml\">RSS Feed</a> - <a href=\"/atom.xml\">Atom Feed</a> - <a href=\"/newsletter/\">Email</a>\n</div>\n<br />\n<div id=\"comicLinks\">\nComics I enjoy:<br/>\n <a href=\"http://threewordphrase.com/\">Three Word Phrase</a>,\n <a href=\"http://www.smbc-comics.com/\">SMBC</a>,\n <a href=\"http://www.qwantz.com\">Dinosaur Comics</a>,\n <a href=\"http://oglaf.com/\">Oglaf</a> (nsfw),\n <a href=\"http://www.asofterworld.com\">A Softer World</a>,\n <a href=\"http://buttersafe.com/\">Buttersafe</a>,\n <a href=\"http://pbfcomics.com/\">Perry Bible Fellowship</a>,\n <a href=\"http://questionablecontent.net/\">Questionable Content</a>,\n <a href=\"http://www.buttercupfestival.com/\">Buttercup Festival</a>,\n <a href=\"http://www.mspaintadventures.com/?s=6&p=001901\">Homestuck</a>,\n\t<a href=\"http://www.jspowerhour.com/\">Junior Scientist Power Hour</a>\n</div>\n<br />\n<div id=\"comicLinks\">\nOther things:<br/>\n <a href=\"https://medium.com/civic-tech-thoughts-from-joshdata/so-you-want-to-reform-democracy-7f3b1ef10597\">Tips on technology and government</a>,<br /> \n <a href=\"https://www.nytimes.com/interactive/2017/climate/what-is-climate-change.html\">Climate FAQ</a>,\n\t<a href=\"https://twitter.com/KHayhoe\">Katharine Hayhoe</a>\n</div>\n<br />\n<center>\n<div id=\"footnote\" style=\"width:70%\">xkcd.com is best viewed with Netscape Navigator 4.0 or below on a Pentium 3&plusmn;1 emulated in Javascript on an Apple IIGS<br />at a screen resolution of 1024x1. Please enable your ad blockers, disable high-heat drying, and remove your device<br />from Airplane Mode and set it to Boat Mode. For security reasons, please leave caps lock on while browsing.</div>\n</center>\n<div id=\"licenseText\">\n<p>\nThis work is licensed under a\n<a href=\"http://creativecommons.org/licenses/by-nc/2.5/\">Creative Commons Attribution-NonCommercial 2.5 License</a>.\n</p><p>\nThis means you're free to copy and share these comics (but not to sell them). <a rel=\"license\" href=\"/license.html\">More details</a>.</p>\n</div>\n</div>\n</body>\n<!-- Layout by Ian Clasbey, davean, and chromakode -->\n</html>\n\n\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"r = rs.get(\"https://xkcd.com/353/\")\n",
71+
"print(r, end=\"\\n\\n\")\n",
72+
"print(r.headers, end=\"\\n\\n\")\n",
73+
"print(r.status_code, end=\"\\n\\n\")\n",
74+
"print(r.ok, end=\"\\n\\n\")\n",
75+
"print(r.text)\n"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 15,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"img = rs.get(\"https://imgs.xkcd.com/comics/python.png\")\n",
85+
"with open(\"./img/comic.png\", \"wb\") as f:\n",
86+
" f.write(img.content) #byte code\n"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 24,
92+
"metadata": {},
93+
"outputs": [
94+
{
95+
"output_type": "stream",
96+
"name": "stdout",
97+
"text": [
98+
"{\n \"args\": {\n \"count\": \"25\", \n \"page\": \"2\"\n }, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"python-requests/2.24.0\", \n \"X-Amzn-Trace-Id\": \"Root=1-5f9305d8-45978a885b4a8c860fd8b5e5\"\n }, \n \"origin\": \"138.0.64.148\", \n \"url\": \"https://httpbin.org/get?page=2&count=25\"\n}\n\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"payload = dict(page=2, count=25)\n",
104+
"r = rs.get(\"https://httpbin.org/get\", params=payload)\n",
105+
"print(r.text)"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 28,
111+
"metadata": {},
112+
"outputs": [
113+
{
114+
"output_type": "stream",
115+
"name": "stdout",
116+
"text": [
117+
"{'password': 'testing', 'username': 'joaovfsousa'}\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"payload = dict(username=\"joaovfsousa\", password=\"testing\")\n",
123+
"r = rs.post(\"https://httpbin.org/post\", data=payload)\n",
124+
"r_dict = r.json()\n",
125+
"print(r_dict.get(\"form\"))"
126+
]
127+
}
128+
]
129+
}

img/comic.png

88.7 KB
Loading

0 commit comments

Comments
 (0)