为什么在pyppeteer中拦截请求不工作?



最小示例:

import asyncio
from pyppeteer import launch
from pyppeteer.network_manager import Request
from pyppeteer_stealth import stealth
browser = await launch()
page = await browser.newPage()
await stealth(page=page)
async def intercept_network_request(request: Request):
print(request.url)
await request.continue_()
await page.setRequestInterception(value=True)
page.on(event='request', f=lambda req: asyncio.ensure_future(intercept_network_request(req)))
response = await page.goto(url='https://google.com/')

输出:

request url: <https://www.google.com/>
---------------------------------------------------------------------------
TimeoutError                              Traceback (most recent call last)
<ipython-input-2-8ccaf6c1ef45> in <module>
----> 1 response = await page.goto(url='https://google.com')
2 # await page.screenshot({'path': 'example.png'})
~/Documents/jupyter/.venv/lib/python3.9/site-packages/pyppeteer/page.py in goto(self, url, options, **kwargs)
883         error = result[0].pop().exception()  # type: ignore
884         if error:
--> 885             raise error
886 
887         request = requests.get(mainFrame._navigationURL)
TimeoutError: Navigation Timeout Exceeded: 30000 ms exceeded.

无论我去哪里,我没有得到一个请求加载。一切工作不拦截请求。有什么问题吗?

禁用用户代理逃税:

await stealth(page, ['user_agent_override'])

见:https://github.com/berstend/puppeteer-extra/issues/421

相关内容

  • 没有找到相关文章