Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the requests Module in Python

Comments

  1. It is suggested that you use the requests module instead of urllib unless you want to have minimal 3rd-party dependencies.

  2. Response.raise_for_status is a convenient method for raising an exception corresponding to the HTTP status code.

In [1]:
import requests
In [2]:
r = requests.get("https://github.com/timeline.json")
In [3]:
payload = {"key1": "value1", "key2": "value2"}
r = requests.post("http://httpbin.org/post")
print(r.text)
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.25.0", 
    "X-Amzn-Trace-Id": "Root=1-5fd9c166-327b33a414f3ed097182b73a"
  }, 
  "json": null, 
  "origin": "24.22.245.193", 
  "url": "http://httpbin.org/post"
}

In [4]:
r.headers
Out[4]:
{'Date': 'Wed, 16 Dec 2020 08:12:22 GMT', 'Content-Type': 'application/json', 'Content-Length': '398', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
In [5]:
import requests
In [6]:
r = requests.get("http://dreamchimney.com")
In [7]:
r.text
Out[7]:
'<!DOCTYPE html>\n<html>\n  <head lang="en">\n    <meta charset="utf-8" />\n    <meta http-equiv="X-UA-Compatible" content="IE=edge" />\n    <meta name="viewport" content="width=device-width, initial-scale=1" />\n\n    <title>&#9733; Dream Chimney &#9733;</title>\n    <meta name="description" content="Music . Dreams . Other Oddball Items" />\n    <meta property="og:title" content="Dream Chimney" />\n    <meta property="og:url" content="https://www.dreamchimney.com/" />\n    <meta property="og:image" content="https://dreamchimney.com/tracks/fb-share-sm.jpg" />\n    <meta property="og:site_name" content="Dream Chimney" />\n    <meta property="og:description" content="Music . Dreams . Other Oddball Items" />\n    <link rel="canonical" href="https://dreamchimney.com" />\n    <link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.php?dc" />\n\n    <script>\n      (function (i, s, o, g, r, a, m) {\n        i["GoogleAnalyticsObject"] = r;\n        (i[r] =\n          i[r] ||\n          function () {\n            (i[r].q = i[r].q || []).push(arguments);\n          }),\n          (i[r].l = 1 * new Date());\n        (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]);\n        a.async = 1;\n        a.src = g;\n        m.parentNode.insertBefore(a, m);\n      })(window, document, "script", "//www.google-analytics.com/analytics.js", "ga");\n\n      ga("create", "UA-95892-1", "dreamchimney.com");\n      ga("send", "pageview");\n    </script>\n\n    <link\n      rel="stylesheet"\n      href="https://use.fontawesome.com/releases/v5.0.8/css/all.css"\n      integrity="sha384-3AB7yXWz4OeoZcPbieVW64vVXEwADiYyAEhwilzWsLw+9FgqpyjjStpPnpBO8o8S"\n      crossorigin="anonymous"\n    />\n    <link href="https://fonts.googleapis.com/css2?family=Righteous&display=swap" rel="stylesheet" />\n\n    <link href="//cdn-images.mailchimp.com/embedcode/classic-10_7.css" rel="stylesheet" type="text/css" />\n    <!-- <script src="home/build/main.js"></script> -->\n    <!-- <link href="home/build/main.css" rel="stylesheet" /> -->\n    <script src="home/build/main.1.0.7.js"></script>\n    <link href="home/build/main.1.0.7.css" rel="stylesheet" />\n  </head>\n  <body id="body-wrapper">\n    <div class="container">\n      <div class="row" id="header">\n        <div class="col-md-6 col-sm-24 hidden-xs hidden-sm">\n          <span>Vol: VXXXVII &#149; No. 3</span>\n          <span>Established: 1997</span>\n          <span>Music . Dreams . Other Oddball Items</span>\n        </div>\n\n        <div class="col-md-12 col-sm-24 text-center">\n          <a href="/"><img src="mainpage/dc-title06.gif" alt="Dream Chimney" class="img-responsive" style="display: inline-block" /></a>\n        </div>\n        <div class="col-md-6 col-sm-24 text-center">\n          <span class="header-infos">\n            <a href="http://twitter.com/dreamchimney" onClick="ga(\'send\', \'event\', \'home-social\', \'DC-Homepage\', \'T\');" target="_blank"\n              ><i class="fab fa-twitter"></i\n            ></a>\n            <a\n              href="http://soundcloud.com/the-dream-chimney"\n              onClick="ga(\'send\', \'event\', \'home-social\', \'DC-Homepage\', \'SC\');"\n              target="_blank"\n              ><i class="fab fa-soundcloud"></i\n            ></a>\n            <a href="http://youtube.com/c/dreamchimney/" onClick="ga(\'send\', \'event\', \'home-social\', \'DC-Homepage\', \'YT\');" target="_blank"\n              ><i class="fab fa-youtube"></i\n            ></a>\n            <a href="http://instagram.com/dreamchimney" onClick="ga(\'send\', \'event\', \'home-social\', \'DC-Homepage\', \'IG\');" target="_blank"\n              ><i class="fab fa-instagram"></i\n            ></a>\n          </span>\n        </div>\n      </div>\n\n      <div class="row">\n        <div id="left-nav" class="col-md-8 text-center">\n          <div id="app-home-image" style="min-height: 300px"></div>\n          <div class="mini-site-nav">\n            <h3>Most Popular</h3>\n            <div>\n              <a href="/tracks/">Track of the Day</a>\n              <a href="/motw">Mix of the Week</a>\n              <a href="/oftheday/">___ of the Day</a>\n              <a href="/sleevery">Sleevery</a>\n              <a href="/store">Store</a>\n              <a href="/today/">Today</a>\n            </div>\n          </div>\n\n          <div class="nav-item">\n            <div style="position: relative; margin-bottom: 10px">\n              <a\n                href="https://dreamchimney.bandcamp.com/album/dream-chimney-demo-tracks-vol-3"\n                target="_blank"\n                style="position: relative; display: inline-block"\n                onClick="ga(\'send\', \'event\', \'DC004\', \'DC-Homepage\', \'dc004\');"\n              >\n                <img src="/mainpage/DC004-homepage-banner.jpg" class="img-responsive center-block" alt="Dream Chimney Demos Cassette" />\n                <span style="position: absolute; bottom: 20px; left: 0; right: 0; padding: 20px; background-color: #fff; opacity: 0.7">\n                  <strong>Out Now: Demo Tracks Vol. 3 &raquo;</strong></span\n                >\n              </a>\n            </div>\n          </div>\n\n          <div class="nav-item">\n            <div class="nav-item feature search-feature text-center">\n              <h2><i class="fa fa-search"></i> <a href="/search/">Search the Chimney</a></h2>\n              <div>\n                <form method="get" action="/search/">\n                  <input type="hidden" name="site" value="www.dreamchimney.com" />\n                  <span>\n                    <input type="text" name="q" maxlength="255" value="" id="search-text" />&nbsp;<button class="icon">\n                      <i class="fa fa-search"></i>\n                    </button>\n                  </span>\n                </form>\n              </div>\n            </div>\n          </div>\n\n          <div class="nav-item">\n            <div class="feature quote-feature">\n              <h2>&#8220;<a href="/quotes">Quote of the Day</a>&#8221;</h2>\n              <div>\n                <span id="app-quote"></span>\n              </div>\n            </div>\n          </div>\n\n          <!-- End Left Nav -->\n        </div>\n        <div class="col-md-15 col-md-offset-1 text-center">\n          <!-- Today -->\n          <div class="list-group nav-item feature most-recent today-feature">\n            <h2><div class="headline">Today on Dream Chimney</div></h2>\n            <!--  -->\n\n            <div id="dc-activity-feed"></div>\n\n            <div class="footer-more text-right">\n              <a href="/today">More recent updates &raquo;</a>\n            </div>\n          </div>\n\n          <!-- Mix of the Week -->\n          <div class="nav-item feature motw-feature">\n            <h2>\n              <a href="/motw"><img src="/mainpage/motw.png?v2" alt="Mix of the Week" /></a>\n              <a href="/motw/" class="title">Mix of the Week</a>\n            </h2>\n            <div class="text-center">\n              <div id="app-motw"></div>\n            </div>\n          </div>\n\n          <!-- Promo Tracks -->\n          <div class="nav-item feature promo-feature">\n            <h2>\n              <a href="/tracks/promo-tracks">DC Promo Tracks</a>\n            </h2>\n            <div id="app-promo-tracks"></div>\n          </div>\n        </div>\n      </div>\n      <!-- \n      <div class="row">\n        <div class="col-xs-24 col-sm-12 col-md-8">\n          <div class="nav-item feature totd-feature">\n            <h2>\n              <a href="/tracks/">Track of the Day</a>\n            </h2>\n            <div>\n              <a href="/tracks/">\n                <img src="mainpage/icon_totd.gif" class="img-responsive dc-icon" alt="Track of the Day" />\n              </a>\n              <span\n                ><a href="/tracks"><div id="app-totd"></div></a\n              ></span>\n            </div>\n          </div>\n        </div>\n        <div class="col-xs-24 col-sm-12 col-md-8">\n          <div class="nav-item feature otd-feature">\n            <h2>\n              <a href="/oftheday/">"____" of the Day</a>\n            </h2>\n            <div>\n              <a href="/oftheday/">\n                <img src="mainpage/icon_otd.gif" class="img-responsive dc-icon" alt="_OTD" />\n              </a>\n              <span>\n                <div id="app-oftheday"></div>\n              </span>\n            </div>\n          </div>\n        </div>\n        <div class="col-xs-24 col-sm-12 col-md-8">\n          \n        </div>\n      </div> -->\n\n      <div class="row">\n        <div class="col-xs-24">\n          <!-- Recent Popular  Tracks -->\n          <div class="nav-item feature promo-feature">\n            <h2>\n              <a href="/tracks/">Popular Tracks from Last Week</a>\n            </h2>\n            <div id="app-recent-popular-tracks"></div>\n          </div>\n        </div>\n      </div>\n      <div class="row">\n        <div class="col-xs-24 col-sm-12 col-md-12">\n          <!-- Sleevery -->\n          <div class="nav-item feature sleevery-feature">\n            <h2>\n              <a href="/sleevery/">Sleevery</a>\n            </h2>\n            <div>\n              <span style="margin-top: 2px; margin-bottom: 10px; line-height: 18px"\n                >The oddest, funniest and simply the best record covers ever made.</span\n              >\n              <a href="/sleevery/">\n                <img\n                  src="https://res.cloudinary.com/dreamchimney/image/upload/w_100/sleevery/3690.jpg"\n                  className="img-responsive dc-icon"\n                  alt="Sleevery"\n                />\n                <img\n                  src="https://res.cloudinary.com/dreamchimney/image/upload/w_100/sleevery/3607.jpg"\n                  className="img-responsive dc-icon"\n                  alt="Sleevery"\n                />\n                <img\n                  src="https://res.cloudinary.com/dreamchimney/image/upload/w_100/sleevery/3650.jpg"\n                  className="img-responsive dc-icon"\n                  alt="Sleevery"\n                />\n                <img\n                  src="https://res.cloudinary.com/dreamchimney/image/upload/w_100/sleevery/3649.jpg"\n                  className="img-responsive dc-icon"\n                  alt="Sleevery"\n                />\n                <img\n                  src="https://res.cloudinary.com/dreamchimney/image/upload/w_100/sleevery/3648.jpg"\n                  className="img-responsive dc-icon"\n                  alt="Sleevery"\n                />\n                <img\n                  src="https://res.cloudinary.com/dreamchimney/image/upload/w_100/sleevery/3685.jpg"\n                  className="img-responsive dc-icon"\n                  alt="Sleevery"\n                />\n              </a>\n            </div>\n          </div>\n        </div>\n        <div class="col-xs-24 col-sm-12 col-md-12">\n          <!-- Interviews  -->\n          <div class="nav-item feature promo-interviews interview-feature">\n            <h2>\n              <a href="/interviews/">Interviews</a>\n            </h2>\n\n            <div style="padding: 10px 0; text-align: center">\n              <span style="margin-bottom: 10px; text-align: left"\n                >This is our chance to have a friendly chimneyside chat with those in music world that interest us the most.</span\n              >\n              <a href="/interviews">\n                <img\n                  src="//www.dreamchimney.com/interviews/_nuxt/img/michoacan.174acc6.jpg"\n                  style="padding: 0"\n                  class="img-responsive interview-image"\n                  alt="Interviews"\n              /></a>\n              <a href="/interviews">\n                <img\n                  src="https://www.dreamchimney.com/_nuxt/img/rinder_the_producer_1977.32e6701.jpg"\n                  style="padding: 0"\n                  class="img-responsive interview-image"\n                  alt="Interviews"\n              /></a>\n              <a href="/interviews">\n                <img\n                  src="https://www.dreamchimney.com/_nuxt/img/FranzAumueller.b485f9a.jpg"\n                  style="padding: 0"\n                  class="img-responsive interview-image"\n                  alt="Interviews"\n              /></a>\n            </div>\n          </div>\n        </div>\n      </div>\n\n      <div class="row">\n        <div class="col-xs-24 col-sm-12 col-md-12">\n          <div class="nav-item feature dreams-feature">\n            <h2>\n              <a href="/dreams/">Dream Theory</a>\n            </h2>\n            <div>\n              <a href="/dreams/"><img src="mainpage/icon_dreams.gif" class="dc-icon" hspace="5" vspace="2" width="60" height="42" /></a>\n              <span>\n                We\'ll analyze, sort and decipher what exactly what your dreams are all about.\n                <a href="/dreams/" style="display: inline-block">Read the most recent dreams</a>\n              </span>\n            </div>\n          </div>\n        </div>\n        <div class="col-xs-24 col-sm-12 col-md-12">\n          <div class="nav-item feature coffee-feature">\n            <h2>\n              <a href="/coffee/">Good Coffee Map</a>\n            </h2>\n            <div>\n              <a href="/coffee"\n                ><img src="https://pbs.twimg.com/profile_images/1096355229/thekindbrew_normal.png" class="dc-icon" alt="Good Coffee Map"\n              /></a>\n              <span>\n                <p>Exact locations specialty coffees are served and served well. <a href="/coffee">View the map</a></p>\n              </span>\n            </div>\n          </div>\n        </div>\n      </div>\n\n      <div class="row">\n        <div class="col-sm-12">\n          <div class="feature question-feature">\n            <h2><a href="/question">Question of the Day</a></h2>\n            <div>\n              <span id="app-question"></span>\n              <br />\n              <a href="/question/latest">Vote now</a>\n            </div>\n          </div>\n        </div>\n        <div class="col-sm-12">\n          <div class="nav-item feature fun-feature">\n            <h2 style="background-color: #000">Music</h2>\n            <div>\n              <span class="flat">\n                <a href="/brokerdealer/">Broker/Dealer</a>, <a href="https://www.sentrall.com/" target="_blank" rel="noopener">SENTRALL</a>,\n                <a href="/procon/">Pro.Con Records</a>,\n                <a href="https://www.sorcerermusic.com/" target="_blank" rel="noopener">Sorcerer</a>,\n                <a href="https://www.beatbroker.net/" target="_blank" rel="noopener">the Beat Broker</a>\n              </span>\n            </div>\n          </div>\n          <div class="nav-item feature fun-feature">\n            <h2 style="background-color: #969">Other / Fun</h2>\n            <div>\n              <span class="flat">\n                <a href="/sleevery/">Sleevery</a>, <a href="/calendar/">Event Calendar</a>, <a href="/bust/">BUST Gallery</a>,\n                <a href="/demodog/">Demo Dog</a>,\n                <a href="http://instagram.com/kind.buds">Kind Buds</a>\n              </span>\n            </div>\n          </div>\n        </div>\n      </div>\n\n      <!-- end container -->\n    </div>\n\n    <footer>\n      <span> Dream Chimney Inc. &copy;2020 -- <a href="/contact/" style="color: #fff">Contact</a> </span>\n    </footer>\n\n    <link href="/global/js/react/activity-feed/build/static/css/main.css" rel="stylesheet" />\n    <script type="text/javascript" src="/global/js/react/activity-feed/build/static/js/main.js"></script>\n    <script type="text/javascript" src="/global/js/react/activity-feed/build/static/js/main2.js"></script>\n    <script type="text/javascript" src="/global/js/react/activity-feed/build/static/js/main3.js"></script>\n  </body>\n</html>\n'
In [8]:
dir(r)
Out[8]:
['__attrs__',
 '__bool__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__nonzero__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_content',
 '_content_consumed',
 '_next',
 'apparent_encoding',
 'close',
 'connection',
 'content',
 'cookies',
 'elapsed',
 'encoding',
 'headers',
 'history',
 'is_permanent_redirect',
 'is_redirect',
 'iter_content',
 'iter_lines',
 'json',
 'links',
 'next',
 'ok',
 'raise_for_status',
 'raw',
 'reason',
 'request',
 'status_code',
 'text',
 'url']
In [9]:
r.ok
Out[9]:
True
In [10]:
r.status_code
Out[10]:
200
In [11]:
?r.ok
Type:        property
String form: <property object at 0x11db981d0>
Docstring:  
Returns True if :attr:`status_code` is less than 400, False if not.

This attribute checks if the status code of the response is between
400 and 600 to see if there was a client error or a server error. If
the status code is between 200 and 400, this will return True. This
is **not** a check to see if the response code is ``200 OK``.
In [ ]:
 

Comments