Hello everyone, today I want to share with you the powerful HTTP request library in Python – Requests. It acts like a “browser” in Python, allowing us to easily send various network requests and retrieve web content. Whether it’s scraping data, calling APIs, or interacting with websites, Requests is your reliable assistant!
1. Installing and Importing Requests
We need to install the Requests library. Open the command line and enter:
pip install requests
After installation, import it in your code:
import requests
2. Sending a GET Request
The GET request is the most commonly used HTTP request method, just like opening a webpage in a browser. Let’s see how to use it:
# Sending a GET request
response = requests.get('https://api.github.com')
print(response.status_code) # Print status code
print(response.text) # Print response content
💡Tip: <span>status_code</span>
of 200 indicates a successful request, 404 indicates the page does not exist, and 500 indicates a server error.
3. Adding Request Parameters
Often we need to add parameters to the URL, such as search keywords. Requests makes this super easy:
# GET request with parameters
params = {
'q': 'python',
'page': 1
}
response = requests.get('https://api.github.com/search/repositories', params=params)
print(response.url) # View the complete URL
4. Handling Response Content
Requests can automatically handle JSON data, which is very convenient:
response = requests.get('https://api.github.com/users/python')
data = response.json() # Convert JSON response to Python dictionary
print(data['name'])
print(data['followers'])
⚠️Note: Before using the <span>json()</span>
method, it’s best to confirm that the response content is in JSON format; otherwise, it may throw an error.
5. Customizing Request Headers
Sometimes websites require specific request headers, such as User-Agent:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
response = requests.get('https://api.github.com', headers=headers)
6. Sending Data with POST Requests
To send data to the server, we use POST requests:
# Sending a POST request
data = {
'username': 'xxx',
'message': 'Hello, World!'
}
response = requests.post('https://httpbin.org/post', data=data)
7. Handling File Uploads
Uploading files is also very simple:
files = {
'file': open('photo.jpg', 'rb')
}
response = requests.post('https://httpbin.org/post', files=files)
💡Tip: Remember to use the <span>with</span>
statement to handle files, as it is safer:
with open('photo.jpg', 'rb') as f:
response = requests.post('https://httpbin.org/post', files={'file': f})
8. Exception Handling
Network requests may fail, so it’s important to handle exceptions:
try:
response = requests.get('https://api.github.com', timeout=5)
response.raise_for_status() # Raise an exception if the status code is not 200
except requests.RequestException as e:
print(f'Request failed: {e}')
Practical Exercise
Try to get the title of a website:
import requests
from bs4 import BeautifulSoup
def get_website_title(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
return soup.title.string
except:
return "Failed to retrieve"
Friends, that’s all for today’s Python learning journey! Remember to code along, and feel free to ask me any questions in the comments. Remember to be polite when making requests to others’ servers and control your request frequency. I wish everyone happy learning and continuous improvement in Python!
Like and share!
Letmoney and love flow to you