如何在Python3中用BeautifulSoup4维持登录会话获取路由器ASP数据
Absolutely! Ditching pyautogui for this task is a great move—GUI automation is brittle (depends on screen size, window focus, etc.), whereas using HTTP session handling is way more reliable and efficient. Routers use cookies to keep track of logged-in sessions, so we can mimic exactly what your browser does using Python's requests library, which automatically manages those cookies for you.
Here's a step-by-step solution to handle the login session programmatically and fetch the neighbour AP data:
Step 1: Install Required Tools
First, make sure you have the requests library installed (it's the standard for HTTP requests in Python):
pip install requests
Step 2: Identify Your Router's Login Details
Before writing code, you need to figure out exactly how your router handles login. Open your browser's DevTools (F12), go to the Network tab, then log into your router GUI. Look for the POST request sent during login (usually to a URL like /login.asp or /cgi-bin/login). Note:
- The full login endpoint URL (e.g.,
http://192.168.10.1/login.asp) - The form fields sent in the request body (typically
username,password, and sometimes hidden fields like a CSRF token orloginType)
Step 3: Write the Session-Aware Script
This script will log into your router, maintain the authenticated session, and fetch/save the neighbour AP data:
import requests # Update these values to match your router ROUTER_IP = "192.168.10.1" LOGIN_ENDPOINT = f"http://{ROUTER_IP}/login.asp" # Replace with your actual login URL NEIGHBOUR_AP_ENDPOINT = f"http://{ROUTER_IP}/html/common/getneighbourAPinfo.asp" USERNAME = "your_router_username" PASSWORD = "your_router_password" # Create a session object to persist cookies across requests session = requests.Session() try: # Prepare login payload (match the fields you saw in DevTools) login_payload = { "username": USERNAME, "password": PASSWORD, # Add any extra fields here (e.g., "csrf_token": "abc123" if required) } # Send login request login_response = session.post(LOGIN_ENDPOINT, data=login_payload) # Verify login success (adjust this check based on your router's response) # You can check status code, or look for a success message in the response if login_response.status_code == 200 and "success" in login_response.text.lower(): print("Login successful! Fetching neighbour AP data...") # Fetch the neighbour AP data using the same authenticated session ap_data_response = session.get(NEIGHBOUR_AP_ENDPOINT) ap_data_response.raise_for_status() # Throw error if request fails # Save the data to a file with open("neighbour_ap_data.json", "w") as output_file: output_file.write(ap_data_response.text) print("Data saved to neighbour_ap_data.json") else: print("Login failed. Check your credentials or login endpoint.") print(f"Response status: {login_response.status_code}") print(f"Preview of response: {login_response.text[:500]}") except requests.exceptions.RequestException as e: print(f"An error occurred: {str(e)}")
Key Notes for Router-Specific Cases
- CSRF Tokens: If your router uses a CSRF token, you'll need to first fetch the login page, extract the token from the HTML, and include it in the login payload. Use
BeautifulSoup(install withpip install beautifulsoup4) to parse the HTML and grab the token. - Encrypted Passwords: Some routers encrypt passwords before sending them. In this case, you'll need to replicate the encryption logic (look at the router's frontend JS code) or use tools like
curlto capture the exact encrypted value. - Testing with Curl: If you want to test manually first, use
curlto replicate the session:# Log in and save cookies to a file curl -c router_cookies.txt -d "username=your_user&password=your_pass" http://192.168.10.1/login.asp # Fetch data using the saved cookies curl -b router_cookies.txt http://192.168.10.1/html/common/getneighbourAPinfo.asp > ap_data.json
内容的提问来源于stack exchange,提问作者Ahsan Farooqui

