我正在尝试使用 Python Pyppeteer 获取 XHR。这是我的代码。
import asyncio
from pyppeteer import launch
import json
async def intercept_response(res):
resourceType = res.request.resourceType
if resourceType in ['xhr']:
resp = await res.text()
try:
r = json.loads(resp)
print(res.request.url)
except:
pass
return res.request.url
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')
await page.waitForSelector('li.item goWork')
await browser.close()
if __name__ == '__main__':
url = asyncio.run(main())
print('IS THIE WAHT YOU WANT:', url)
但是当我运行它时,浏览器永远不会关闭,并且在 30 秒后,它给了我一个 TimeoutError。并且代码应该返回 xhr 响应的 url,但它没有。