python-3.x - Python(Pyppeteer):[WinError 10054] 远程主机强制关闭现有连接
问题描述
我正在尝试使用 Python 通过本地服务器设置提供一些 HTML,然后使用该pyppeteer
模块截取网页的屏幕截图。
这是截取屏幕截图的 Python 代码。
from pyppeteer import launch
async def main():
browser = await launch(headless=True)
page = await browser.newPage()
arg = sys.argv[1]
await page.goto("http://localhost:8888/map.html"), {'waitUntil': 'networkidle2'})
await page.pdf({'path': 'screenshot.pdf', 'landscape' : True, 'format': 'A4'});
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
这是map.html
我试图通过 Python 服务器提供的代码。它读入一个带有一些功能设置和调用的 json 文件,flex_map
其中包含一些 D3 Javascript 代码。
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="/js/legend.js"></script>
<script src="/js/flex_map.js"></script>
</head>
<body>
<script>
d3.json("/settings/map_settings.json").then(function(mapSettings) {
flex_map(mapSettings.geoJsonFile,
mapSettings.dataFile,
mapSettings.fillVariable,
mapSettings.geoVariable,
mapSettings.geoJsonGeoVariable,
mapSettings.legend_title,
mapSettings.legendTranslateX,
mapSettings.legendTranslateY);
});
</script>
</body>
</html>
当我在创建本地服务器后提交此代码时python take_screenshot.py
,我收到以下错误:
Serving HTTP on 0.0.0.0 port 8888 (http://0.0.0.0:8888/) ...
127.0.0.1 - - [23/Apr/2020 18:34:02] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Apr/2020 18:34:04] "GET /map.html HTTP/1.1" 304 -
127.0.0.1 - - [23/Apr/2020 18:34:04] "GET /settings/map_settings.json HTTP/1.1" 304 -
127.0.0.1 - - [23/Apr/2020 18:34:04] "GET /data/temp_data.csv HTTP/1.1" 304 -
127.0.0.1 - - [23/Apr/2020 18:35:08] "GET /map.html HTTP/1.1" 200 -
127.0.0.1 - - [23/Apr/2020 18:35:08] "GET /js/legend.js HTTP/1.1" 200 -
127.0.0.1 - - [23/Apr/2020 18:35:08] "GET /js/flex_map.js HTTP/1.1" 200 -
127.0.0.1 - - [23/Apr/2020 18:35:08] "GET /settings/map_settings.json HTTP/1.1" 200 -
127.0.0.1 - - [23/Apr/2020 18:35:09] "GET /data/temp_data.csv HTTP/1.1" 200 -
127.0.0.1 - - [23/Apr/2020 18:35:09] "GET /geoJSON/us_counties.json HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52861)
Traceback (most recent call last):
File "C:\Anaconda\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "C:\Anaconda\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Anaconda\lib\http\server.py", line 646, in __init__
super().__init__(*args, **kwargs)
File "C:\Anaconda\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Anaconda\lib\http\server.py", line 426, in handle
self.handle_one_request()
File "C:\Anaconda\lib\http\server.py", line 394, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Anaconda\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
我不确定这个问题的根源可能在哪里。一件奇怪的事情是,如果我将 HTML 文件更改为
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="/js/legend.js"></script>
<script src="/js/flex_map.js"></script>
</head>
<body>
<script>
flex_map(args go here...);
</script>
</body>
</html>
代码运行良好并截取了屏幕截图,因此它可能与加载 json 文件有关,但我不确定如何解决该问题。map.html
通过本地主机在 Chrome 上打开代码也可以正常工作。
解决方案
推荐阅读
- android - 在Android中调用Activity.setRequestedOrientation(...)时出现黑屏
- java - 如何在不使用拆分、子字符串或索引的情况下在某个字符处剪切字符串?
- c# - 如何使 endDate 的日期仅在 startdate 的日期之后选择 ASP.NET
- python - 将值附加到特定列 openpyxl
- django - django-storages + private S3 bucket - 如何将用户名设为文件夹名称?
- google-cloud-platform - 翻译 API 命令行快速入门失败
- scheme - 检查 Scheme 中的排列
- sql - 创建一个用户定义的函数来返回一个包含客户信息的表?
- javascript - 谁能以一种易于理解的方式逐字翻译这个 JavaScript 函数?
- r - 如何将变量放在 Rstudio 的目录中?