如何使用Python Requests修改casamundo搜索框输入值并提交搜索查询(不使用Selenium)
Got it, let's break down why your current approach isn't working and how to fix it.
Why Your data={'value': 'England'} Method Fails
That readonly input with data-test="autocomplete-input" is just a UI display element—its value only shows the destination you've selected, not the actual parameter the server uses to process searches. When you send value: England via Requests, the server doesn't recognize it because the real search request uses entirely different parameters (often tied to internal destination IDs, not plain text).
Step 1: Find the Actual Search API
First, you need to uncover the backend API that handles Casamundo's search requests:
- Open your browser's DevTools (F12) and switch to the Network tab.
- Go to the Casamundo search page, enter a destination (e.g., "France"), and click search.
- Look for POST/GET requests labeled with terms like
search,results, orquery. Check the request's:- Request URL: The actual endpoint the search is sent to.
- Form Data/Query Parameters: The key-value pairs the server expects (e.g.,
destinationId,checkIn,guests). - Headers: Critical details like
User-Agent,Referer, andCookie(many sites block requests without these).
Step 2: Simulate the API Request with Requests
Once you have the correct endpoint and parameters, you can replicate the request directly. Here's a sample based on typical Casamundo API behavior:
import requests # Create a session to persist cookies (helps avoid being blocked) session = requests.Session() # Copy headers from your browser's DevTools (adjust these to match your request) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', 'Referer': 'https://www.casamundo.com/search/', 'Accept': 'application/json, text/plain, */*' } # Example payload (replace with parameters from your DevTools capture) # Note: Many sites use destination IDs instead of plain text—get this from autocomplete API if needed search_payload = { 'destinationId': 'GBR-ENG', # Example ID for England 'destinationName': 'England', 'checkIn': '2024-07-15', 'checkOut': '2024-07-22', 'adults': 2, 'children': 0 } # Send the request to the actual API endpoint response = session.post( 'https://www.casamundo.com/api/v2/search', # Replace with your captured endpoint headers=headers, json=search_payload # Use json= instead of data= if the request uses JSON format ) # Process the response if response.status_code == 200: results = response.json() print("Search results loaded successfully!") # Extract relevant data from results here else: print(f"Request failed: Status code {response.status_code}") print(response.text)
Pro Tips
- Get Destination IDs: If the API requires a destination ID (most do), look for an autocomplete API request when you type in the search box. It’ll return a list of destinations with their corresponding IDs.
- Update Headers: Always use real browser headers to avoid being flagged as a bot. You can copy these directly from DevTools' "Request Headers" section.
- Handle Cookies: Using a
Sessionobject ensures cookies are persisted, which helps maintain a valid session with the site.
内容的提问来源于stack exchange,提问作者Frogleim1

