Difficulty: Beginner
Estimated Time: 10 minutes

Python Requests

When your browser loads a website it's asking the server that's hosting the site for a page. This is called a request. This is what a request for a webpage might look like:

GET /index.html HTTP/1.1
  Host: malverncode.club
  Connection: keep-alive
  Upgrade-Insecure-Requests: 1
  User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  DNT: 1
  Referer: https://www.google.co.uk/
  Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
  

Then the server sends back a response with the page inside which might look like this:

HTTP/1.1 200 OK
  Date: Mon, 27 Jul 2009 12:28:53 GMT
  Server: Apache/2.2.14 (Win32)
  Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
  Content-Length: 88
  Content-Type: text/html
  Connection: Closed
  
<html>
      <p>Hey</p>
  </html>
  

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Python Requests

Step 1 of 2

Currency Converter

Now we're going to use Python to get exchange rate from a website.

We're going to use a website called Fixer.io which is a really easy to use exchange rate API.

In your editor open app.py which is the file where you'll write your code.

You can execute your program by using python3 app.py, though there will be prompts when to test your code.

The editor saves automatically every few seconds.

Firstly, you need to import the urllib module for Python which lets us, as the name suggests, interact with websites. You can import the module with import urllib.request (click to copy code).

Next we can get the website by doing this:

urllib.request.urlopen("YOUR_URL_HERE").read()

Insert a URL from the Fixer.io page where it says YOUR URL HERE.

Now, surround the line to get the website with print( ) to show it to the console.

We can now try out our code with python3 app.py (you can also click this).

The output doesn't look very useful does it? We can fix that by parsing the data with the JSON module because the data the website responds with is in JSON format. Import it with import json.

Now surround the inside of the print command with json.loads( ) which will make the data into something which is easier to manipulate.

That line is now getting a little messy so space it out a bit and you should have something like this:

import urllib.request
import json

data = urllib.request.urlopen("http://api.fixer.io/latest?base=GBP").read()
data = json.loads(data)

print(data)

Now we can index the data variable to get the exchange rate for GB Pounds to US Dollars by putting ["rates"] on the end of the data variable and then doing the same with USD. Try it out and you should get something like this when you run it python3 app.py :

1.2459 (as of 30/3/17).

Challenges

  • Ask the user for a value in pounds and convert it to dollars.
  • Ask the user for a value and the currency and convert it to dollars.
  • Ask the user for a value and the currency and convert it to anther currency the user has inputted.
App