[Python] using playwright page.route function, some http headers disappear is this normal?

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to get playwright to be as close to a regular chrome browser instance as it can be, while also speeding up the loading by remodeling unneeded requests for things like images. While doing so I noticed that when I use the route function my http headers change substantially. Updated playwright and browsers as well.

    from playwright.sync_api import sync_playwright

    def main():

    with sync_playwright() as playwright:
    browser = playwright.chromium.launch(channel='chromium', headless=False)
    context = browser.new_context()

    page = context.new_page()
    # page.route('**/*.{.jpg|png}', lambda route: route.abort())

    resp = page.goto('https://www.google.com')

    print(resp.request.all_headers())

    if __name__ == '__main__':
    main()


    if you run the code with the line uncommented the headers change from the original.

    original headers:

    {':authority': 'www.google.com', ':method': 'GET', ':path': '/', ':scheme': 'https', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', 'accept-encoding': 'gzip, deflate,
    br, zstd', 'accept-language': 'en-US,en;q=0.9', 'priority': 'u=0, i', 'sec-ch-ua': '"Chromium";v="131", "Not_A Brand";v="24"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', 'sec-fetch-dest': 'document', 'sec-fetch-mode': 'navigate', 'sec-fetch-site': 'none', 'sec-fetch-user': '?1', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'}


    with routing:

    {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'sec-ch-ua': '"Chromium";v="131", "Not_A Brand";v="24"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"'}

    Continue reading...

Compartilhe esta Página