Document
Document
概述
Document 是 Aspose.Html FOSS 中用于 Python 的类。继承自:Node。
DOM 树的根节点。
此类提供 66 个用于在 Python 程序中操作 Document 对象的方法。可用的方法包括:__init__、add_event_listener、adopt_node、append、append_child、attach_style_sheet、clone_node、close、compare_document_position、contains、create_attribute、create_attribute_ns,以及另外 54 个方法。安装 Aspose.Html FOSS for Python 包后,所有公共成员对任何 Python 应用程序均可访问。属性:a_link_color、active_element、adopted_style_sheets、all、anchors、applets,以及另外 67 项。
属性
| 名称 | 类型 | 访问 | 描述 |
|---|---|---|---|
default_view | `` | 读取 | 每个文档的 Window 对象(懒加载,已缓存)。 |
location | `` | 读取 | 返回关联的 Window 的 Location 对象,或 None。 |
implementation | "DOMImplementation" | 读取 | 每个文档的 DOMImplementation 对象(懒加载,已缓存)。 |
dom_config | DOMConfiguration | 读取 | 每个文档的 DOMConfiguration 对象(惰性,缓存)。 |
url | str | 读取 | 文档 URL(WHATWG Document.URL),默认值 "about:blank"。 |
document_uri | str | 读取 | 文档的 URL 作为字符串(WHATWG DOM §4.5 documentURI)。 |
base_uri | str | 读取 | 已解析此文档的基础 URL(WHATWG DOM Node.baseURI)。 |
referrer | str | 读取 | 引用文档的 URL(WHATWG HTML 标准 §document)。 |
domain | str | 读取 | 文档 URL 的主机名组件(WHATWG HTML 标准 §document)。 |
last_modified | str | 读取 | 最后修改时间戳字符串(WHATWG HTML 标准 §document)。 |
cookie | str | 读写 | 只读 cookie 字符串存根 — 始终返回 ""(WHATWG HTML §8.11.1)。 |
hidden | bool | 读取 | 文档是否隐藏 — 在无头模式下始终为 False。 |
visibility_state | str | 读取 | 文档可见性状态 — 在无头模式下始终为 "visible"。 |
design_mode | str | 读写 | 文档是否处于设计模式(WHATWG HTML §7.6.4)。 |
dir | str | 读写 | 文档文本方向 (WHATWG HTML §3.3.2)。 |
current_script | None | 读取 | 当前执行的脚本元素 (WHATWG HTML §8.1.3.4)。 |
character_set | str | 读取 | 解析期间使用的字符编码(WHATWG DOM 第3.1节)。 |
charset | str | 读取 | 别名为 :attr:character_set(HTML 标准名称)。 |
input_encoding | str | 读取 | 别名为 :attr:character_set(WHATWG DOM 兼容名称)。 |
content_type | str | 读取 | 文档 MIME 类型;对于 HTML 文档始终为 "text/html"。 |
style_sheets | "StyleSheetList" | 读取 | 实时文档样式表列表。 |
adopted_style_sheets | list["CSSStyleSheet"] | 读写 | 文档级可构建样式表采用列表。 |
node_name | str | 读取 | '#document' |
document_element | `Element | None` | 读取 |
document_type | `DocumentType | None` | 读取 |
compat_mode | str | 读写 | 'BackCompat' (quirks) 或 'CSS1Compat' (no-quirks)。 |
ready_state | str | 读取 | 文档加载状态。 |
parse_errors | list[ParseError] | 读取 | 上一次解析操作的解析错误。 |
head | `Element | None` | 读取 |
body | `Element | None` | 读取 |
forms | "HTMLCollection" | 读取 | 实时收集文档中所有 <form> 元素。 |
images | "HTMLCollection" | 读取 | 实时收集文档中所有 <img> 元素。 |
links | "HTMLCollection" | 读取 | 实时收集 <a href> 和 <area href> 元素。 |
scripts | "HTMLCollection" | 读取 | 文档中所有 <script> 元素的实时集合。 |
embeds | "HTMLCollection" | 读取 | 所有 <embed> 后代的实时集合。 |
applets | "HTMLCollection" | 读取 | 所有 <applet> 后代的实时集合。 |
anchors | "HTMLCollection" | 读取 | 实时收集 <a name=...> 元素(传统命名锚点)。 |
title | str | 读写 | 在 <head> 中第一个 <title> 元素的文本内容。 |
compatible_mode | str | 读取 | 怪异模式指示器(WHATWG HTML §3.2.1)。 |
strict_error_checking | bool | 读写 | 始终在无头模式下 True(DOM3 Core §1.1.1)。 |
first_element_child | `“Element | None”` | 读取 |
last_element_child | `“Element | None”` | 读取 |
child_element_count | int | 读取 | 直接子节点中是 Element 节点的数量。 |
active_element | `“Element | None”` | 读取 |
full_screen_enabled | bool | 读取 | 始终 False — 在无头模式下无显示 (Fullscreen API §7). |
fullscreen_element | None | 读取 | 始终 None — 在无头模式下没有全屏元素 (Fullscreen API §7). |
pointer_lock_element | None | 读取 | 始终 None — 在无头模式下没有指针锁定 (Pointer Lock API §4). |
timeline | "_DocumentTimeline" | 读取 | 文档时间轴存根;current_time 始终为 0.0(Web Animations API §6.2)。 |
all | "_AllCollection" | 读取 | 所有元素按树的顺序(WHATWG HTML §legacy document layout interface)。 |
plugins | "HTMLCollection" | 读取 | 根据 WHATWG HTML §4.1 legacy interface,别名为 :attr:embeds。 |
children | "HTMLCollection" | 读取 | 文档的直接元素子节点 (WHATWG DOM §4.2.6 ParentNode)。 |
origin | str | 读取 | 不透明的来源字符串 — 在无头模式下始终为 'null' (WHATWG DOM §4.5)。 |
fg_color | str | 读写 | 通过 <body> text 属性反映已废弃的 fgColor 文档级属性。 |
bg_color | str | 读写 | 通过 <body> bgcolor 属性,反映已废弃的 bgColor 文档级属性。 |
link_color | str | 读写 | 通过 <body> link 属性,反映已废弃的 linkColor 文档级属性。 |
v_link_color | str | 读写 | 通过 <body> vlink 属性,反映已废弃的 vlinkColor 文档级属性。 |
a_link_color | str | 读写 | 通过 <body> alink 属性,反映已过时的 alinkColor 文档级属性。 |
fonts | `` | 读取 | 在无头模式下始终 None。 |
picture_in_picture_enabled | bool | 读取 | 在无头模式下始终 False。 |
picture_in_picture_element | `` | 读取 | 始终在无头模式下 None。 |
prerendering | bool | 读取 | 始终在无头模式下 False。 |
node_type | int | 读取 | 来自 NodeType 的整数节点类型常量。 |
node_value | `str | None` | 读写 |
text_content | `str | None` | 读写 |
parent_node | `Node | None` | 读取 |
parent_element | `Element | None` | 读取 |
is_connected | bool | 读取 | 此节点是否是文档的节点树的一部分(WHATWG DOM §4.4)。 |
owner_document | `Document | None` | 读取 |
child_nodes | NodeList | 读取 | 所有子节点的实时 NodeList。 |
first_child | `Node | None` | 读取 |
last_child | `Node | None` | 读取 |
previous_sibling | `Node | None` | 读取 |
next_sibling | `Node | None` | 读取 |
方法
| 签名 | 描述 |
|---|---|
__init__() | Calls init 在此 Document 实例上。 |
normalize_document() | 使用现有的 Node.normalize 语义对该文档进行规范化。 |
has_focus() → bool | 返回文档是否获得焦点 — 在无头模式下始终为 False。 |
open(type: str, replace: str) → "Document" | 以写入方式打开文档(WHATWG HTML §7.7)。 |
close() | 写入后关闭文档 (WHATWG HTML §7.7)。 |
write() | 将字符串写入文档流 (WHATWG HTML §7.7)。 |
writeln() | 写入字符串并换行 (WHATWG HTML §7.7)。 |
attach_style_sheet(sheet: "CSSStyleSheet") | 将样式表附加到此文档。 |
detach_style_sheet(sheet: "CSSStyleSheet") | 分离先前附加的样式表。 |
| `save(path: “str | pathlib.Path”, encoding: str)` |
create_element(tag_name: str) → "HTMLElement" | 创建属于此文档的 HTMLElement。 |
create_element_ns(namespace_uri: str, qualified_name: str) → Element | 在特定命名空间中创建由此文档拥有的 Element。 |
create_text_node(data: str) → Text | 使用给定的字符数据创建一个 Text 节点。 |
create_comment(data: str) → Comment | 使用给定的字符数据创建一个 Comment 节点。 |
create_cdata_section(data: str) → "CDATASection" | 创建一个由该文档拥有的 CDATASection 节点。 |
create_document_fragment() → DocumentFragment | 创建一个由该文档拥有的空 DocumentFragment。 |
create_event(interface: str) → "Event" | 创建一个遗留事件对象用于 interface. |
create_range() → "Range" | 在此文档中创建一个折叠的 Range。 |
get_selection() → "Selection" | 返回此文档的单例选择对象。 |
create_processing_instruction(target: str, data: str) → "ProcessingInstruction" | 创建一个由此文档拥有的 ProcessingInstruction 节点。 |
create_attribute(local_name: str) → "Attr" | 创建一个由此文档拥有的非命名空间 Attr。 |
| `create_attribute_ns(namespace_uri: str | None, qualified_name: str)→“Attr”` |
| `create_tree_walker(root: “Node”, what_to_show: int, node_filter: “Callable[[Node], int] | None”)→“TreeWalker”` |
| `create_node_iterator(root: “Node”, what_to_show: int, node_filter: “Callable[[Node], int] | None”)→“NodeIterator”` |
adopt_node(node: Node) → Node | Adopt node 将其移动到此文档中,从当前的树中移出。 |
import_node(node: Node, deep: bool) → Node | 返回一个克隆 node 此文档拥有的。 |
get_element_by_id(id: str) → `Element | None` |
get_elements_by_tag_name(name: str) → HTMLCollection | 返回具有给定标签名的所有后代元素。 |
get_elements_by_class_name(names: str) → HTMLCollection | 返回具有所有给定类名的所有后代元素。 |
filter_fn(el: Element) → bool | 在此 Document 实例上调用 filter_fn(el)。 |
get_elements_by_name(name: str) → NodeList | 返回所有后代元素,其 name 属性等于 name. |
| `get_elements_by_tag_name_ns(namespace: “str | None”, local_name: str)→list` |
prepend() | Insert nodes 在此文档的第一个子节点之前。 |
append() | Append nodes 作为此文档的最后子节点。 |
replace_children() | 删除此文档的所有子节点,然后追加 nodes. |
query_selector(selector: str) → `Element | None` |
query_selector_all(selector: str) → NodeList | 返回所有匹配 CSS 选择器的元素的静态 NodeList。 |
get_animations() → list[object] | 返回文档级别的动画(headless stub: always [])。 |
element_from_point(x: float, y: float) | 始终 None — headless 没有用于点击测试的布局引擎 (CSSOM View §11.6)。 |
elements_from_point(x: float, y: float) → list | 始终 [] — headless 没有用于点击测试的布局引擎 (CSSOM View §11.6)。 |
exec_command(command_id: str, show_ui: bool, value: str) → bool | 始终 False — 传统编辑命令;在无头环境中没有管道 (WHATWG HTML §8.6.1)。 |
query_command_enabled(command_id: str) → bool | 始终 False — 在无头环境中没有编辑管线 (WHATWG HTML §8.6.1)。 |
has_storage_access() → bool | 始终 True — 在无头环境中,origin 被视为同源 (Storage Access API)。 |
request_storage_access() | 空操作桩 — 在无头环境中始终拥有存储访问权限;无需异步授权 (Storage Access API)。 |
get_root_node(composed: bool) → "Node" | 返回节点树的根。 |
has_child_nodes() → bool | 返回此节点是否至少有一个直接子节点。 |
| `lookup_namespace_uri(prefix: str | None)→str |
| `lookup_prefix(namespace_uri: str | None)→str |
| `is_default_namespace(namespace_uri: str | None)→bool` |
| `is_supported(feature: str, version: str | None)→bool` |
| `get_feature(feature: str, version: str | None)→object |
| `set_user_data(key: str, data: object | None, handler: object |
get_user_data(key: str) → `object | None` |
append_child(node: Node) → Node | Append node 作为此节点的最后一个子节点。 |
| `insert_before(node: Node, reference: Node | None)→Node` |
remove_child(node: Node) → Node | Remove node 从子列表中取出并返回它。 |
replace_child(new_child: Node, old_child: Node) → Node | Replace old_child with new_child. |
clone_node(deep: bool) → Node | 返回此节点的副本。 |
normalize() | 将此节点及其整个子树置于规范形式。 |
| `contains(other: Node | None)→bool` |
compare_document_position(other: "Node") → int | 返回一个描述位置的位掩码 other 相对于此节点。 |
| `is_equal_node(other: Node | None)→bool` |
| `is_same_node(other_node: Node | None)→bool` |
add_event_listener(type: str, listener: Callable, capture: bool, once: bool) | Register listener 用于事件的 type. |
remove_event_listener(type: str, listener: Callable, capture: bool) | 注销先前已注册的监听器。 |
dispatch_event(event: "Event") → bool | Dispatch event 通过祖先树。 |