You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取域名第三方Cookie并获取其名称与值?求解决方案

How to Extract Third-Party Cookies for a Specific Domain (Name & Value)

Hey there, I totally get why this is frustrating—Selenium's default get_cookies() method only pulls cookies tied to the current top-level domain, which misses all those third-party ones from embedded resources like ads, analytics tools, or social widgets. Let's break down two reliable solutions to fix this:

Solution 1: Selenium + Chrome DevTools Protocol (CDP)

Chrome (and Edge) support the Chrome DevTools Protocol, which lets you bypass Selenium's limitations and access all cookies stored by the browser—including third-party ones. Here's a complete Python example:

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

def fetch_third_party_cookies(target_domain, target_page):
    # Configure Chrome options (disable third-party cookie blocking if needed)
    chrome_options = Options()
    chrome_options.add_argument("--disable-features=ThirdPartyCookieBlocking")
    # Optional: Keep browser open for debugging
    # chrome_options.add_experimental_option("detach", True)

    # Initialize Chrome driver
    driver = webdriver.Chrome(options=chrome_options)
    driver.get(target_page)

    # Enable the Network domain in CDP
    driver.execute_cdp_cmd("Network.enable", {})

    # Fetch ALL cookies (including third-party)
    all_cookies = driver.execute_cdp_cmd("Network.getAllCookies", {})["cookies"]

    # Get current page's domain to filter out non-third-party cookies
    current_domain = driver.execute_script("return document.domain")

    # Filter cookies matching your target third-party domain
    third_party_cookies = []
    for cookie in all_cookies:
        # Adjust the match logic based on your needs (e.g., exact domain, suffix match)
        if target_domain in cookie["domain"] and current_domain not in cookie["domain"]:
            third_party_cookies.append({
                "name": cookie["name"],
                "value": cookie["value"],
                "domain": cookie["domain"]  # Optional: include domain for verification
            })

    driver.quit()
    return third_party_cookies

# Usage example
target_domain = "analytics.example.com"
target_page = "https://your-website.com"  # Page that loads the third-party resource
cookies = fetch_third_party_cookies(target_domain, target_page)

print(f"Found {len(cookies)} third-party cookies for {target_domain}:")
for cookie in cookies:
    print(f"Name: *{cookie['name']}*, Value: `{cookie['value']}`")

Key Notes:

  • CDP Command: Network.getAllCookies is the magic here—it returns every cookie the browser has stored for the current session, regardless of domain.
  • Filtering Logic: We exclude cookies from the current page's domain to ensure we only get true third-party ones. Adjust the domain match (e.g., use cookie["domain"].endswith(target_domain) for exact suffix matches) based on your needs.
  • Privacy Settings: Some browsers block third-party cookies by default—adding --disable-features=ThirdPartyCookieBlocking ensures you can access them in your automation session.

Solution 2: Use Playwright (Simpler Alternative)

If you're open to switching tools, Playwright natively supports fetching all cookies (including third-party) without extra work. It's more modern and often easier for these edge cases:

from playwright.sync_api import sync_playwright

def get_third_party_cookies_playwright(target_domain, target_page):
    with sync_playwright() as p:
        # Launch browser with third-party cookies enabled
        browser = p.chromium.launch(args=["--disable-features=ThirdPartyCookieBlocking"])
        context = browser.new_context()
        page = context.new_page()
        page.goto(target_page)

        # Get all cookies across all domains
        all_cookies = context.cookies()

        # Filter third-party cookies
        current_domain = page.evaluate("document.domain")
        third_party_cookies = [
            {"name": c["name"], "value": c["value"]}
            for c in all_cookies
            if target_domain in c["domain"] and current_domain not in c["domain"]
        ]

        browser.close()
        return third_party_cookies

# Usage example
cookies = get_third_party_cookies_playwright("analytics.example.com", "https://your-website.com")
for cookie in cookies:
    print(f"Cookie Name: *{cookie['name']}*, Value: `{cookie['value']}`")

Why This Works:

Playwright's context.cookies() method returns all cookies associated with the browser context, not just the current page's domain. It's designed to handle modern web scenarios like third-party resources out of the box.

Important Considerations

  • Browser Compatibility: The CDP method works best with Chrome/Edge. For Firefox, you'd need to use WebDriver BiDi instead (since Firefox's CDP support is limited).
  • Cookie Expiry & Scope: Make sure you're accessing the page that actually loads the third-party resource—cookies are only set when the resource is requested.
  • Legal & Ethical: Always ensure you're complying with privacy laws (like GDPR) when accessing or handling user cookies.

内容的提问来源于stack exchange,提问作者Sumati Malhotra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:02:26