I am trying to return a list of XHR urls from Python Async. Below is my code.
import asyncio
from pyppeteer import launch
async def intercept_response(res):
resourceType = res.request.resourceType
xhr_list = []
if resourceType in ['xhr']:
print(res.request.url)
xhr_list.append(res.request.url)
return xhr_list
async def main():
browser = await launch(headless=False)
page = await browser.newPage()
page.on('response', intercept_response)
await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1')
await page.goto('https://www.iesdouyin.com/share/user/70015326114', waitUntil = 'networkidle2')
await browser.close()
if __name__ == '__main__':
url = asyncio.run(main())
print(url)
However, when I run the code, res.request.url got printed out, but xhr_list is not returned, causing url to be None. Is there something wrong with my code?
urlwill be assigned whatever value you return frommain. Since you don't return anything from it,urlis set toNone.