make a http get request in python

Here's an example of how to make an HTTP GET request in Python using the urllib library:

main.py
import urllib.request

url = 'https://www.example.com'
response = urllib.request.urlopen(url)
content = response.read().decode()

print(content)
145 chars
8 lines

In this example, we imported the urllib.request module which provides a simple way to make HTTP requests in Python. We then specified the URL we want to request (https://www.example.com) and used the urlopen() method to send the GET request.

Once we have the response object, we can read the content of the response using the read() method, which returns a byte string. We then decode this byte string into a regular string using the decode() method.

Finally, we print the content of the response to the console.

related categories

gistlibby LogSnag