如何发送含空格单词的GET请求?requests库请求异常疑问
Hey Bob, let's break down why your request isn't returning the content you expect—this is a common gotcha with how the requests library handles parameter values!
The Root Problem
You set the query parameter to a list [brand, style], but Poshmark's search endpoint expects a single string where terms are joined by a + (like freepeople+top). When you pass a list to requests.get() for a parameter, the library will generate a URL with duplicate query parameters, like this:https://poshmark.com/search?query=freepeople&query=top&type=listings&department=Women
That's not what the site's search system is looking for, so it returns unexpected results.
Corrected Code
Here's how to fix it by joining your brand and style into a single string for the query parameter:
import requests search = 'https://poshmark.com/search?' brand = "freepeople" style = "top" # Join brand and style with a + to match the expected query format queryParameters = { 'query': f"{brand}+{style}", 'type': 'listings', 'department': 'Women' } # Optional: Add a User-Agent header to mimic a browser (some sites block default 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' } response = requests.get(search, params=queryParameters, headers=headers) # Verify the generated URL matches your target print(response.url)
Quick Check
After running this, print(response.url) should output exactly the URL you intended:https://poshmark.com/search?query=freepeople+top&type=listings&department=Women
This should make the request match what you'd manually enter in a browser, so the response content should align with your expectations.
内容的提问来源于stack exchange,提问作者Bob

