2
  1. how to set headers for pyppeteer for example:Accept-Encoding: gzip
  2. how to print pyppeteer headers in python. i know java
const response = await page.goto('https://example.org/')
console.log(response.headers)

result

{ date: 'Sun, 29 Oct 2017 13:35:59 GMT',
'content-encoding': 'gzip',
'last-modified': 'Fri, 09 Aug 2013 23:54:35 GMT',
server: 'ECS (lga/1318)',
etag: '"359670651+gzip"',
vary: 'Accept-Encoding',
'x-cache': 'HIT',
'content-type': 'text/html',
status: '200',
'cache-control': 'max-age=604800',
'content-length': '606',
expires: 'Sun, 05 Nov 2017 13:35:59 GMT' }
4

1 回答 1

2
import pyppeteer
import asyncio
from pyppeteer.network_manager import Request, Response

async def req_intercept(req: Request):
    print(f'Original header: {req.headers}')
    req.headers.update({'Accept-Encoding': 'gzip'})
    await req.continue_(overrides={'headers': req.headers})

async def resp_intercept(resp: Response):
    print(f"New header: {resp.request.headers}")


async def test():
    browser = await pyppeteer.launch()
    page = await browser.newPage()
    await page.setRequestInterception(True)
    page.on('request', req_intercept)
    page.on('response', resp_intercept)
    resp = await page.goto('https://example.org/')
    print(resp.headers)

asyncio.get_event_loop().run_until_complete(test())

结果:

Original header: {'upgrade-insecure-requests': '1', 'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/69.0.3494.0 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'}
New header:      {'upgrade-insecure-requests': '1', 'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/69.0.3494.0 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip'}
{'status': '200', 'content-encoding': 'gzip', 'accept-ranges': 'bytes', 'cache-control': 'max-age=604800', 'content-type': 'text/html; charset=UTF-8', 'date': 'Sat, 13 Apr 2019 03:07:49 GMT', 'etag': '"1541025663"', 'expires': 'Sat, 20 Apr 2019 03:07:49 GMT', 'last-modified': 'Fri, 09 Aug 2013 23:54:35 GMT', 'server': 'ECS (dcb/7F84)', 'vary': 'Accept-Encoding', 'x-cache': 'HIT', 'content-length': '606'}
于 2019-04-13T02:51:37.517 回答