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_configDOMConfiguration读取每个文档的 DOMConfiguration 对象(惰性,缓存)。
urlstr读取文档 URL(WHATWG Document.URL),默认值 "about:blank"。
document_uristr读取文档的 URL 作为字符串(WHATWG DOM §4.5 documentURI)。
base_uristr读取已解析此文档的基础 URL(WHATWG DOM Node.baseURI)。
referrerstr读取引用文档的 URL(WHATWG HTML 标准 §document)。
domainstr读取文档 URL 的主机名组件(WHATWG HTML 标准 §document)。
last_modifiedstr读取最后修改时间戳字符串(WHATWG HTML 标准 §document)。
cookiestr读写只读 cookie 字符串存根 — 始终返回 ""(WHATWG HTML §8.11.1)。
hiddenbool读取文档是否隐藏 — 在无头模式下始终为 False。
visibility_statestr读取文档可见性状态 — 在无头模式下始终为 "visible"。
design_modestr读写文档是否处于设计模式(WHATWG HTML §7.6.4)。
dirstr读写文档文本方向 (WHATWG HTML §3.3.2)。
current_scriptNone读取当前执行的脚本元素 (WHATWG HTML §8.1.3.4)。
character_setstr读取解析期间使用的字符编码(WHATWG DOM 第3.1节)。
charsetstr读取别名为 :attr:character_set(HTML 标准名称)。
input_encodingstr读取别名为 :attr:character_set(WHATWG DOM 兼容名称)。
content_typestr读取文档 MIME 类型;对于 HTML 文档始终为 "text/html"。
style_sheets"StyleSheetList"读取实时文档样式表列表。
adopted_style_sheetslist["CSSStyleSheet"]读写文档级可构建样式表采用列表。
node_namestr读取'#document'
document_element`ElementNone`读取
document_type`DocumentTypeNone`读取
compat_modestr读写'BackCompat' (quirks) 或 'CSS1Compat' (no-quirks)。
ready_statestr读取文档加载状态。
parse_errorslist[ParseError]读取上一次解析操作的解析错误。
head`ElementNone`读取
body`ElementNone`读取
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=...> 元素(传统命名锚点)。
titlestr读写在 <head> 中第一个 <title> 元素的文本内容。
compatible_modestr读取怪异模式指示器(WHATWG HTML §3.2.1)。
strict_error_checkingbool读写始终在无头模式下 True(DOM3 Core §1.1.1)。
first_element_child`“ElementNone”`读取
last_element_child`“ElementNone”`读取
child_element_countint读取直接子节点中是 Element 节点的数量。
active_element`“ElementNone”`读取
full_screen_enabledbool读取始终 False — 在无头模式下无显示 (Fullscreen API §7).
fullscreen_elementNone读取始终 None — 在无头模式下没有全屏元素 (Fullscreen API §7).
pointer_lock_elementNone读取始终 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)。
originstr读取不透明的来源字符串 — 在无头模式下始终为 'null' (WHATWG DOM §4.5)。
fg_colorstr读写通过 <body> text 属性反映已废弃的 fgColor 文档级属性。
bg_colorstr读写通过 <body> bgcolor 属性,反映已废弃的 bgColor 文档级属性。
link_colorstr读写通过 <body> link 属性,反映已废弃的 linkColor 文档级属性。
v_link_colorstr读写通过 <body> vlink 属性,反映已废弃的 vlinkColor 文档级属性。
a_link_colorstr读写通过 <body> alink 属性,反映已过时的 alinkColor 文档级属性。
fonts``读取在无头模式下始终 None。
picture_in_picture_enabledbool读取在无头模式下始终 False。
picture_in_picture_element``读取始终在无头模式下 None。
prerenderingbool读取始终在无头模式下 False。
node_typeint读取来自 NodeType 的整数节点类型常量。
node_value`strNone`读写
text_content`strNone`读写
parent_node`NodeNone`读取
parent_element`ElementNone`读取
is_connectedbool读取此节点是否是文档的节点树的一部分(WHATWG DOM §4.4)。
owner_document`DocumentNone`读取
child_nodesNodeList读取所有子节点的实时 NodeList。
first_child`NodeNone`读取
last_child`NodeNone`读取
previous_sibling`NodeNone`读取
next_sibling`NodeNone`读取

方法

签名描述
__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: “strpathlib.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: strNone, 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) → NodeAdopt node 将其移动到此文档中,从当前的树中移出。
import_node(node: Node, deep: bool) → Node返回一个克隆 node 此文档拥有的。
get_element_by_id(id: str) → `ElementNone`
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: “strNone”, local_name: str)→list`
prepend()Insert nodes 在此文档的第一个子节点之前。
append()Append nodes 作为此文档的最后子节点。
replace_children()删除此文档的所有子节点,然后追加 nodes.
query_selector(selector: str) → `ElementNone`
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: strNone)→str
`lookup_prefix(namespace_uri: strNone)→str
`is_default_namespace(namespace_uri: strNone)→bool`
`is_supported(feature: str, version: strNone)→bool`
`get_feature(feature: str, version: strNone)→object
`set_user_data(key: str, data: objectNone, handler: object
get_user_data(key: str) → `objectNone`
append_child(node: Node) → NodeAppend node 作为此节点的最后一个子节点。
`insert_before(node: Node, reference: NodeNone)→Node`
remove_child(node: Node) → NodeRemove node 从子列表中取出并返回它。
replace_child(new_child: Node, old_child: Node) → NodeReplace old_child with new_child.
clone_node(deep: bool) → Node返回此节点的副本。
normalize()将此节点及其整个子树置于规范形式。
`contains(other: NodeNone)→bool`
compare_document_position(other: "Node") → int返回一个描述位置的位掩码 other 相对于此节点。
`is_equal_node(other: NodeNone)→bool`
`is_same_node(other_node: NodeNone)→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") → boolDispatch event 通过祖先树。

另请参阅

 中文