我正在尝试从 Python Async 返回 XHR url 列表。下面是我的代码。
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)
但是,当我运行代码时, res.request.url 被打印出来,但没有返回 xhr_list,导致 url 为 None。我的代码有问题吗?