如何从XHR获取目标URL?及如何用Requests从页面获取指定API地址?
Hey there! Let's tackle your two questions step by step—first pulling target URLs from XHR requests, then using the Requests library to fetch data from that specific API.
1. Extracting Target URLs from XHR Requests
Getting XHR request URLs is straightforward with your browser's developer tools. Here's how:
- Fire up your browser's DevTools (use F12, or Ctrl+Shift+I on Windows/Linux, Cmd+Opt+I on Mac)
- Switch to the Network tab at the top
- Filter the requests to only show XHR (look for the XHR filter option in the bar above the request list)
- Refresh the page or trigger the action that loads the content you're targeting (like scrolling, clicking a button)
- Browse through the list of XHR requests—check the Name or Request URL columns to spot your target. Click on a request to see full details.
- Head to the Headers tab of the selected request to get the full, exact URL, plus any headers the request used (you might need these later for the Requests library!)
2. Fetching Data from the Specified API with Requests
First things first: Make sure you have the requests library installed. If you don't, run this in your terminal:
pip install requests
Now, here's a code snippet to fetch content from that API. A quick heads-up: Many APIs expect specific headers (like a User-Agent string) to return valid data—you can grab these headers from the XHR request you inspected earlier (in the DevTools Headers tab):
import requests # Your target API URL api_url = "https://www.holonis.com/api/comments/5ae3cfc82eebca0025b9890c/post" # Optional: Add headers if the API requires them (customize based on what you saw in DevTools) 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", # If the API needs an Authorization token or other headers, add them here } try: # Send a GET request to the API response = requests.get(api_url, headers=headers) # Raise an error if the request failed (e.g., 404, 500 status codes) response.raise_for_status() # Parse the JSON response (most APIs return JSON data) api_data = response.json() # Print or process the data however you need print("Successfully fetched API content:") print(api_data) except requests.exceptions.RequestException as e: print(f"Oops, something went wrong: {e}")
- If the API uses a POST method instead of GET, just swap
requests.get()withrequests.post()and include any required payload (you can find this in the Payload tab of the XHR request in DevTools).
内容的提问来源于stack exchange,提问作者Rajas Rasam
相关产品推荐
相关产品推荐

