You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python/Selenium如何捕获网站发起的网络请求?

Capture m3u8 Stream URLs with Selenium (No Proxies Needed)

Great question! You absolutely don’t need proxies or third-party tools to grab m3u8 requests with Selenium. And while you could automate the DevTools UI manually, there’s a far more reliable and efficient way using Chrome DevTools Protocol (CDP)—which Selenium integrates with directly.

Here’s how to do it:

The key is to leverage Selenium’s ability to interact with CDP to listen for network requests in real-time, then filter for URLs ending in .m3u8.

Step-by-Step Code Example (Python)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

# Initialize Chrome options
chrome_options = Options()
# Optional: Keep browser open after script ends for debugging
# chrome_options.add_experimental_option("detach", True)

# Start the driver with CDP support
driver = webdriver.Chrome(options=chrome_options)

# Enable Network monitoring via CDP
driver.execute_cdp_cmd("Network.enable", {})

# Store captured m3u8 URLs
m3u8_urls = []

# Define a callback to capture requests
def capture_m3u8(request):
    url = request.get("params", {}).get("request", {}).get("url", "")
    if ".m3u8" in url:
        if url not in m3u8_urls:
            m3u8_urls.append(url)
            print(f"Found m3u8 URL: {url}")

# Add the listener for request events
driver.add_cdp_listener("Network.requestWillBeSent", capture_m3u8)

# Navigate to your target page
driver.get("https://your-target-video-site.com")

# Wait for the video to load (adjust time as needed, or use explicit waits)
time.sleep(10)

# Disable network monitoring
driver.execute_cdp_cmd("Network.disable", {})

# Print all captured m3u8 URLs
print("\nAll captured m3u8 URLs:")
for url in m3u8_urls:
    print(url)

# Cleanup
driver.quit()

Key Notes:

  • Why CDP instead of automating DevTools UI? Automating the DevTools tab (clicking around, filtering requests) is fragile—small UI changes in Chrome could break your script. CDP is a stable, official API that lets you directly hook into the browser’s network layer without relying on UI interactions.
  • Filtering Requests: The example uses Network.requestWillBeSent to catch requests before they’re sent, but you could also use Network.responseReceived if you want to verify the response status code first.
  • Wait Strategies: Instead of time.sleep(), use Selenium’s explicit waits (e.g., waiting for a video element to be visible) to make the script more robust.
  • Browser Compatibility: This method works with Chrome and Edge (since both use Chromium). For Firefox, you’d use the Firefox DevTools Protocol, which has similar functionality but slightly different commands.

Can you automate the DevTools UI directly?

Technically, yes—you could use Selenium to open the DevTools tab (with driver.execute_script("window.open('devtools://devtools/bundled/inspector.html')")), click the Network tab, and filter for m3u8. But this is not recommended because:

  • DevTools UI elements are not part of the page DOM, so they’re harder to target reliably.
  • Chrome often updates DevTools layout, which would break your selectors.
  • It’s slower and less efficient than using CDP.

Stick with the CDP method—it’s the industry standard for this kind of task.

内容的提问来源于stack exchange,提问作者Flying Thunder

火山引擎 最新活动