im trying to open a website in chrome whith pyppeteer, capture all the requests the website makes and look at the headers. If my code finds a specific header, it should close the browser and stop runnig.
my code:
import asyncio
import json
import time
from pyppeteer import launch
async def intercept_network_requests(request):
for key in request.headers:
if 'some_header_name' in key:
print('Got header value: ',request.headers[key])
#now i want to close the browser and stop the script
async def main():
browser = await launch(headless=False, autoclose=False)
page = await browser.newPage()
page.on('request', lambda request: asyncio.ensure_future(intercept_network_requests(request)))
await page.goto('https://example.com')
time.sleep(10000)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
the script works, i get the string im looking for,but its just runs forever, im kinda new in python and not sure how asyncio works,
i tried to put the page.on('request'
into while loop and set some variable true when i find my header, but in that case it would never continue to await page.goto
line
how to do this the right way ?