Python爬虫发送Request Body数据咨询:VBA转Python入门问题
Hey there! As someone who’s got solid VBA/WinHTTP experience and is just dipping their toes into Python for web scraping, I get how it can feel a bit overwhelming at first. Let’s break down how to send request bodies in Python—starting with the most straightforward, beginner-friendly tools that’ll feel familiar to your existing workflow.
First off, forget the low-level urllib module for now—requests is the Python equivalent of WinHTTP, but way more intuitive and less verbose. It’s the go-to library for almost all web scraping/API tasks, so let’s start there.
Step 1: Install the Requests Library
First, install it via pip (Python’s package manager) if you haven’t already:
pip install requests
Common Request Body Scenarios (With Code Examples)
Let’s cover the most common cases you’d encounter, mapped to what you might already do in VBA:
1. Sending JSON Formatted Bodies
This is like when you’d construct a JSON string in VBA and send it with WinHTTP, setting the Content-Type: application/json header. In Python, requests handles the serialization and header setup automatically.
import requests # Define your payload as a Python dictionary (way easier than building strings in VBA!) payload = { "product_id": 12345, "quantity": 2, "customer_details": { "name": "John Doe", "email": "john@example.com" } } # Send the POST request—note the `json` parameter instead of `data` response = requests.post("https://example.com/api/place-order", json=payload) # Check the response (equivalent to WinHTTP's responseText) print("Response Status Code:", response.status_code) print("Response Content:", response.text)
2. Sending Form-Encoded Bodies (application/x-www-form-urlencoded)
This is the standard form submission format—like when you’d send a string like username=foo&password=bar in VBA. Again, requests simplifies this:
import requests # Form data as a dictionary payload = { "username": "your_username", "password": "your_secure_password" } # Use the `data` parameter here—requests sets the correct Content-Type automatically response = requests.post("https://example.com/login", data=payload) # Access the response just like before print(response.text)
3. Sending Multipart Form Data (For File Uploads or Mixed Data)
If you’ve ever had to upload files via VBA (a huge pain!), Python makes this trivial. This is for multipart/form-data requests, which combine regular form fields and file attachments.
import requests # Regular form fields form_data = { "upload_description": "This is a test text file" } # Define the file to upload—use `open()` in read-binary mode # Pro tip: Use a `with` statement to auto-close the file after the request with open("test_file.txt", "rb") as file_to_upload: files = {"file": file_to_upload} # Send the request with both `data` (form fields) and `files` (attachments) response = requests.post("https://example.com/upload", data=form_data, files=files) print(response.text)
4. Custom Raw Request Bodies
If you ever need to send a raw string (like a custom format that doesn’t fit JSON or form data), you can do that too—similar to how you’d send a raw string in WinHTTP:
import requests # Your custom body string raw_body = "custom_key=custom_value&special_format=yes" # Manually set the Content-Type header headers = { "Content-Type": "application/x-www-form-urlencoded" } # Send with the `data` parameter and your custom headers response = requests.post("https://example.com/custom-endpoint", data=raw_body, headers=headers) print(response.text)
Bonus: Adding Custom Headers (Like User-Agent or Auth Tokens)
Just like in VBA where you’d use WinHttpRequest.SetRequestHeader, you can add custom headers in requests with a dictionary:
import requests headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", "Authorization": "Bearer your_auth_token_here" } payload = {"key": "value"} response = requests.post("https://example.com/api", json=payload, headers=headers)
Final Notes for VBA Users
- Unlike VBA’s single-threaded limitation, Python has great async options later on (like
aiohttp) if you need to send multiple requests in parallel—but start withrequestsfirst to get the basics down. requestshandles cookies, redirects, and error handling way more gracefully than WinHTTP, so you’ll spend less time debugging low-level issues.
内容的提问来源于stack exchange,提问作者Singularity20XX

