def innerHTML(element):
"""Returns the inner HTML of an element as a UTF-8 encoded bytestring"""
return element.encode_contents()
这些函数目前不在联机文档中,因此我将引用代码中的当前函数定义和文档字符串。
encode_contents-从4.0.4开始
def encode_contents(
self, indent_level=None, encoding=DEFAULT_OUTPUT_ENCODING,
formatter="minimal"):
"""Renders the contents of this tag as a bytestring.
:param indent_level: Each line of the rendering will be
indented this many spaces.
:param encoding: The bytestring will be in this encoding.
:param formatter: The output formatter responsible for converting
entities to Unicode characters.
"""
def decode_contents(self, indent_level=None,
eventual_encoding=DEFAULT_OUTPUT_ENCODING,
formatter="minimal"):
"""Renders the contents of this tag as a Unicode string.
:param indent_level: Each line of the rendering will be
indented this many spaces.
:param eventual_encoding: The tag is destined to be
encoded into this encoding. This method is _not_
responsible for performing that encoding. This information
is passed in so that it can be substituted in if the
document contains a <META> tag that mentions the document's
encoding.
:param formatter: The output formatter responsible for converting
entities to Unicode characters.
"""
美化组3
BeautifulSoup 3没有上述功能,而是有renderContents
def renderContents(self, encoding=DEFAULT_OUTPUT_ENCODING,
prettyPrint=False, indentLevel=0):
"""Renders the contents of this tag as a string in the given
encoding. If encoding is None, returns a Unicode string.."""
如果只需要文本(不需要HTML标记),则可以使用
.text
:TL;博士
对于BeautifulSoup 4,如果希望使用UTF-8编码的testring,请使用
element.encode_contents()
;如果希望使用Python Unicode字符串,请使用element.decode_contents()
。例如,DOM's innerHTML method可能看起来像这样:这些函数目前不在联机文档中,因此我将引用代码中的当前函数定义和文档字符串。
encode_contents
-从4.0.4开始另请参见documentation on formatters;您很可能使用
formatter="minimal"
(默认)或formatter="html"
(对于html entities),除非您希望以某种方式手动处理文本。encode_contents
返回已编码的bytestring。如果需要Python Unicode字符串,请改用decode_contents
。decode_contents
-从4.0.1开始decode_contents
与encode_contents
执行相同的操作,但返回的是Python Unicode字符串,而不是经过编码的bytestring。美化组3
BeautifulSoup 3没有上述功能,而是有
renderContents
为了与BS3兼容,这个函数被添加回BeautifulSoup 4(in 4.0.4)。
其中一个选择是使用类似的方法:
相关问题 更多 >
编程相关推荐