javascript - 如何使用请求获取 Javascript 页面的完整 HTML 内容?
问题描述
我想使用 Python 来检索 JavaScript 后面的一些内容。我有一个结构如下的网站:(为了便于阅读,我添加了换行符):
'<!DOCTYPE html>\n<html lang="ja">\n <head>\n <meta charset="utf-8"/>\n <title>HRMOS CORE Employee</title>\n
<base href="/"/>\n <meta name="viewport" content="width=device-width, initial-scale=1"/>\n
<link rel="icon" type="image/x-icon" href="assets/favicon.ico"/>\n <link rel="stylesheet" href="styles.ea399ef1dfe4d0c1ba1f.css"></head>\n
<body class="pol">\n
<ess-root></ess-root>\n <script src="runtime-es2015.9968ad6faaffe0609121.js" type="module"></script><script src="runtime-es5.9968ad6faaffe0609121.js" nomodule defer></script><script src="polyfills-es5.1271ae52020e40d8a200.js" nomodule defer></script><script src="polyfills-es2015.04cc7054da2685d59767.js" type="module"></script><script src="main-es2015.07a2df0e93ddc04b405f.js" type="module"></script><script src="main-es5.07a2df0e93ddc04b405f.js" nomodule defer></script></body>\n</html>\n'
实际的 html 内容是一个交互式表格,由前面代码中的 js 脚本呈现。
我正在尝试使用该request
模块检索表的内容。我使用了以下代码(我确定授权正在工作,因为请求模块返回状态 200):
from requests_html import AsyncHTMLSession
import lxml.html
session = AsyncHTMLSession()
r = await session.get('https://ess.hrmos.co/', auth=('user', 'password'))
await r.html.arender(wait=10, sleep=10, keep_page=True)
tree = lxml.html.fromstring(r.text)
title_elem = tree.xpath("/html/body/ess-root/core-ui-theme/ess-core-layout/pol-layout/pol-content/ess-employee-list-page/div/div/ess-employee-cards-container/cdk-virtual-scroll-viewport/div[1]/div[1]/a[1]/ess-employee-card/pol-card/div[1]")
print("title tag:", title_elem.tag)
但即使我在代码中使用了等待,我从中得到的实际 htmlrequest
也是空的,其中没有任何内容。有没有办法确保我检索内容?还是我必须使用硒?
解决方案
推荐阅读
- python - 如何在 python 中使用不同模式在谷歌大查询中附加数据框?
- python - 使用 SMTP 使用 python 将多个附件从 gmail 发送到 gmail?
- angular - 使用 @HostBinding 来确定 CSS 变量值的 Angular 组件在某些 Angular 应用程序中有效,而在其他应用程序中无效
- spring-integration - Spring Integration:如何检查输入的 wsdl 消息是否有效?
- npm - 如何更改 npm 模块 jsmediatags 的最大字符串长度
- spring - @WithMockUser 从静态枚举或字符串中读取值
- javascript - 如何在不知道其索引的情况下从数组中取出一个对象,只知道“键”名称
- c - 一元'*'的无效类型参数(有int)
- python - SyntaxError:Linux 中 Jupyter Notebook 上的语法无效
- python - Hist2D 的圆形框架?