| 123456789101112131415161718192021222324252627282930313233 |
- import httpx
- # 定义Splash服务器的URL
- SPLASH_URL = 'http://localhost:8050'
- # 定义Splash的API端点
- RENDER_HTML_ENDPOINT = SPLASH_URL + '/render.html'
- # 要访问的网页URL
- TARGET_URL = 'http://www.baidu.com'
- # 创建httpx客户端
- client = httpx.Client()
- # 发送POST请求到Splash,请求渲染TARGET_URL
- response = client.post(RENDER_HTML_ENDPOINT, data={'url': TARGET_URL})
- # 关闭客户端连接
- client.close()
- # 检查请求是否成功
- if response.status_code == 200:
- # 获取渲染后的HTML内容
- html_content = response.text
- print('HTML Content:')
- print(html_content)
- else:
- print('Failed to render the page:', response.status_code)
- exit(0)
- # 可选:保存渲染后的HTML到文件
- with open('baidu.html', 'w', encoding='utf-8') as file:
- file.write(html_content)
|