This README provides a comprehensive guide on fetching internet resources, decoding responses, and making HTTP requests in Python using urllib and requests packages.
urllib is a Python module that provides a set of URL handling modules. To fetch internet resources, you can use the following steps:
import urllib.request
url = "https://example.com"
response = urllib.request.urlopen(url)
content = response.read()
print(content)To decode the body response from urllib, you can use the following:
decoded_content = content.decode("utf-8")
print(decoded_content)requests is a popular third-party library that simplifies HTTP requests. Install it using:
pip install requestsTo make a GET request with requests:
import requests
url = "https://example.com"
response = requests.get(url)
print(response.text)For other HTTP methods like POST, PUT, etc., you can use:
data = {"key": "value"}
response = requests.post(url, data=data)
print(response.text)To fetch JSON resources and parse them:
response = requests.get("https://api.example.com/data.json")
json_data = response.json()
print(json_data)Once you have the data, you can manipulate it based on your requirements. For example:
for item in json_data["items"]:
print(item["name"])