我的 pyppeteer 连接意外关闭,它让我的 Python 程序挂起,而不是关闭并正确记录错误。有谁知道如何正确捕获此异常并正确退出 Python 程序?
这是我的脚本的一部分:
async def _proxy_browser(self, url, headless = False):
args = []
for i in range(5):
try:
browser = await launch(headless = headless,
args = args,
defaultViewport = None)
page = await browser.newPage()
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0')
await page.goto(url, {'waitUntil' : 'domcontentloaded'})
time.sleep(5)
content = await page.content()
return content
except (pyppeteer.errors.PageError, pyppeteer.errors.TimeoutError) as e:
print('error', e)
time.sleep(2)
continue
finally:
time.sleep(1)
await browser.close()
return
def proxy_browser_request(self, url, headless = False):
loop = asyncio.get_event_loop()
response = loop.run_until_complete(self._proxy_browser(url, headless))
return response
这是我收到的回溯:
[E:pyppeteer.connection] connection unexpectedly closed
Task exception was never retrieved
future: <Task finished name='Task-1379' coro=<Connection._async_send() done, defined at C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\pyppeteer\connection.py:69> exception=InvalidStateError('invalid state')>
Traceback (most recent call last):
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\pyppeteer\connection.py", line 73, in _async_send
await self.connection.send(msg)
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\websockets\protocol.py", line 567, in send
await self.write_frame(True, opcode, data)
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\websockets\protocol.py", line 1083, in write_frame
await self.ensure_open()
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\websockets\protocol.py", line 803, in ensure_open
raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\pyppeteer\connection.py", line 79, in _async_send
await self.dispose()
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\pyppeteer\connection.py", line 170, in dispose
await self._on_close()
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\site-packages\pyppeteer\connection.py", line 151, in _on_close
cb.set_exception(_rewriteError(
asyncio.exceptions.InvalidStateError: invalid state