Python Requests HTTP Module Primer Tutorial

Python Requests HTTP Module Primer Tutorial

Python Requests HTTP Module Primer Tutorial

Python being the great serverside language it is, caters well for functionality involving the HTTP protocol, the protocol of the Internet. Perhaps you recall the usage of the Python “HTMLParser” when we presented Python HTML Attribute Parsing Primer Tutorial involving a web application parsing HTML. Today we freshly install the Python “requests” module in our local MacBook Pro Mac OS X command line environment, and then up at the Linux web server at the rjmprogramming.com.au domain.

We like to think of the Python “requests” module (HTML for Humans) in relation to two contexts …

  • imagining you are “surfing the net” at the web browser address bar … the URL that you type (or get derived) here is the major argument for a Python call to a Python application (on the command line or perhaps pointed at by PHP exec or passthru or some other means) … a Python requests module call can glean what that resultant webpage (or JSON conversion of that) content will be
  • both “GET” and “POST” methods within an HTML webpage form navigating to a webpage can be defined with a Python requests module call to glean what the webpage (or JSON conversion of that) content would be

Why not just “do” the navigation rather than “model” it via a Python “requests” module call? Same reason PHP’s file_get_contents($url) is constantly of use, as you may be sick of us talking about at this blog. You often want to know the contents of a webpage without necessarily navigating there, to glean some information or construct an email, or to piece together a report or document … the list goes on. So much so, that we have called our “hello world Python requests module” file_get_contents.py and we have written a PHP supervisory link to it that shows you this Python requests derived recreation of the RJM Programming News webpage ...

As ever, we hope this is of interest to you.


Previous relevant Python HTML Attribute Parsing Primer Tutorial is shown below.

Python HTML Attribute Parsing Primer Tutorial

Python HTML Attribute Parsing Primer Tutorial

Let’s follow up yesterday’s Python parsing of XML (Python XML Primer Tutorial) with a bit of HTML parsing (on the Linux command line) … believe it or not this came second in I.T. history thinking … first thoughts were about XML parsing … (I’ll get up when you stop waving that feather).

We use …


from HTMLParser import HTMLParser

… but you will see other good Python HTML parsing products called Beautiful Soup and Element Soup module.

Today we parse some HTML attribute data (which you can also do with XML data with the “get” method).

We found all the links below of great use, and for that we say “thanks”.

Link to downloadable Python programming source code which can be renamed to parse_html_attribute.py here, that, with its example, worked with the input x.html file and/or (MAMP) URL content data.

What is great about Python (or PHP or Perl or lots of other interpretative languages) with Linux (or Windows, for that matter), for me anyway, is the variety of command line access (piped) approaches it opens up. Today’s code could be accessed via …

  • echo [inputContent] | python parse_html_attribute.py [attributeType]
  • python parse_html_attribute.py [[inputContentFile] [attributeType]]
  • python parse_html_attribute.py [attributeType] [ < inputContentFile]
  • python parse_html_attribute.py [[inputContentURL] [attributeType]]
  • python parse_html_attribute.py # this does nothing, after 5 seconds (timeout)

… amongst many other “spawn” or (PHP) “exec” possibilities.

Hope you give Python a go to parse away today.


Previous relevant Python XML Primer Tutorial is shown below.

Python XML Primer Tutorial ... http://www.python.org/

Python XML Primer Tutorial ... http://www.python.org/

The Python programming language has great modules for parsing and manipulating XML data, from the “old days” PyXML, now not actively supported, to the ElementTree functionality we work with, today, to show you some parsing of some very simple XML input data. Perhaps you want to read a previous Python tutorial here at Python List Primer Tutorial as shown below to get a feel for Python again, should you be a beginner here. So below Wikipedia talks about Python …

Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.

In this primer tutorial you can see some XML Purchase Order data being parsed and each of its Part Numbers (or you often see the term “Sku”) being changed to write out new XML where the Part Numbers get a prefix of “x” from what they used to be.

Here is another Python tutorial called Python Processing Directory Filespec Primer Tutorial.

Today’s tutorial uses a Linux desktop environment, but Python (with Django for example) can be your “goto” web server language or can be the CGI language of choice with PHP, for example, to be useful for web applications.

Link to downloadable Python programming source code which can be renamed to product_parse.py here, that works with the input product_parse.xml file data (resulting in out.xml) with the command …


python product_parse.py < product_parse.xml

Link to Python website … Home of Python … where quote up the top resides
Link to a very useful Python ElementTree tutorial which helped with this tutorial, so, thanks … Useful Python ElementTree tutorial
Link to Python jobs done … Python jobs done … personal experience
And yes, it’s true … the language name is a fond reference to Monty Python


Previous relevant Python List Primer Tutorial is shown below.

Python List Primer Tutorial ... http://www.python.org/

Python List Primer Tutorial ... http://www.python.org/

In this tutorial we examine some functionality code snippets suiting Python list data structure, in response to, really, and extending, a Yahoo Answers question. Lists, like tuples, are interesting in that different base data types that can’t cohabit an array can be part of a list, or tuple. This tutorial builds on the Python Primer Tutorial of previous times, presented below. Let’s see immediately below what Wikipedia says about the very interesting Python programming language.

Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.

In this primer tutorial you can see some list usage in Python for a Linux Bash session via the Mac laptop application Terminal.

Here is another Python tutorial called Python Processing Directory Filespec Primer Tutorial.

Link to downloadable Python programming source code which can be renamed to listwork.py here.

Want to see it as a live run? This is more problematic with cgi-bin default language of Perl and server-side language of PHP, as at our web server. However there is a way to redirect the output of a PHP exec call of the Python into some HTML and show that (easiest in the root directory of the website), and then this (kind of awkward (… but would be pretty easy to make it less awkward)) live run is possible. The programming PHP source code for this is listwork.php here. The Three P’s ride again (Perl is sick today, so rides as a hologram)! But she’s a gem (chortle, chortle).

Link to Python website … Home of Python … where quote up the top resides
Link to a very useful Python tuple tutorial which helped with this tutorial, so, thanks … Useful Python tuple tutorial
Link to Python jobs done … Python jobs done … personal experience
And yes, it’s true … the language name is a fond reference to Monty Python


Previous Python Primer Tutorial is shown below.

Python Primer Tutorial ... http://www.python.org/

Python Primer Tutorial ... http://www.python.org/

Have you heard of Python?

Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.

Have used it for Games Programming and it is great at parsing XML via the SAX API.

Python has some really interesting data structures such as tuples, lists, sets, dictionaries for instance … read more here.

In this primer tutorial you can see some snapshots showing examples of use.

Here is another Python tutorial called Python Processing Directory Filespec Primer Tutorial.

Link to Python website … Home of Python … where quote up the top resides
Link to Python jobs done … Python jobs done … personal experience
And yes, it’s true … the language name is a fond reference to Monty Python


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *