Learn Python: How to make GET Web Request

In the past I have published few articles on how to make HTTP Web Requests with programming languages like C#, JavaScript, VB.Net etc. In the post, I am going to show how do you make Web Request using Python.

requests Package

Like any other language, first question that will come to your mind is what kind of packages are available in Python for http based requests. In Python, you will be using requests package.

pip install requests

get function

You will use get function to get response from a target URL. Following sample shows how to get latest BitCoin price using CoinDesk API URL.

# Make sure that "requests" package is installed.
# To install "requests" package, use following command in Windows
# pip install requests
import requests
from types import SimpleNamespace
import json

class WebApp:
    def __init__(self):
        self.bitcoin_feedurl = "https://api.coindesk.com/v1/bpi/currentprice.json"

    def get_bitcoin_prices(self):
        # Send GET request to target uri
        response = requests.get(self.bitcoin_feedurl)
        # Response data is JSON feed.
        # Decode data feed.
        bitcoin_price = json.loads(response.text, object_hook=lambda c: SimpleNamespace(**c))
        print(f"Price at {bitcoin_price.time.updated} = ${bitcoin_price.bpi.USD.rate_float}")
        return bitcoin_price

webApp = WebApp()

You can read more about BitCoin price feed widget development using .Net core from my other post.




15.8 °C / 60.4 °F

weather conditions Clear

Monthly Posts

Blog Tags