现在,我想要打印该
如果您需要一个 UTF-8 编码的字节字符串,请使用 BeautifulSoup 4 中的 element.encode_contents()
方法,如果您需要一个 Python Unicode 字符串,则使用 element.decode_contents()
方法。例如,DOM 的 innerHTML 方法 可能会像这样:
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.
"""
另请参阅格式化程序文档。你最有可能使用formatter="minimal"
(默认值)或formatter="html"
(用于HTML实体),除非你希望以某种方式手动处理文本。
encode_contents
返回一个已编码的字节串。如果你想要Python Unicode字符串,则应使用decode_contents
。
decode_contents
- 自4.0.1版本起decode_contents
与encode_contents
做的事情相同,但返回的是Python Unicode字符串而不是已编码的字节串。
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.
"""
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.."""
为了与BS3兼容,此函数已于BeautifulSoup 4 (在4.0.4中)重新添加。
其中一个选项可以是使用类似这样的东西:
innerhtml = "".join([str(x) for x in div_element.contents])
给定一个类似于<div id="outer"><div id="inner">foobar</div></div>
的BS4 soup元素,以下是可以用于以不同方式检索其HTML和文本的各种方法和属性,以及它们返回的示例。
InnerHTML:
inner_html = element.encode_contents()
'<div id="inner">foobar</div>'
外部HTML:
outer_html = str(element)
'<div id="outer"><div id="inner">foobar</div></div>'
OuterHTML(格式化后):
pretty_outer_html = element.prettify()
'''<div id="outer">
<div id="inner">
foobar
</div>
</div>'''
仅文本(使用.text):
element_text = element.text
'foobar'
仅文本(使用.string):
element_string = element.string
'foobar'
str(element)
可以帮助您获取outerHTML,然后从外部HTML字符串中删除外部标记。
inner_html = soup.find('body').children
它将返回一个列表。因此,您可以使用简单的for循环获取完整代码。
for html in inner_html:
print(html)
"".join(map(str,soup.find('body').children)).strip()
- Setopunicode(x)
就可以了。对我来说起作用。
编辑:这将为您提供外部HTML而不是内部。<div class="test">
text in body
<p>Hello World!</p>
</div>
text in body
<p>Hello World!</p>
''.join(map(str,tag.contents))
get_text()
如果你只想要一个文档或标签内可读的文本,你可以使用get_text()
方法。它将返回一个包含文档或标签下所有文本的Unicode字符串:
markup = '<a href="http://example.com/">\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup, 'html.parser')
soup.get_text()
'\nI linked to example.com\n'
soup.i.get_text()
'example.com'
你可以指定一个字符串来连接文本的各个部分:
soup.get_text("|")
'\nI linked to |example.com|\n'
你可以告诉Beautiful Soup去除每个文本块开头和结尾的空格:
soup.get_text("|", strip=True)
'I linked to|example.com'
但是在那个时候,您可能想要使用.stripped_strings
生成器,然后自己处理文本:
[text for text in soup.stripped_strings]
# ['I linked to', 'example.com']
<script>
、<style>
和<template>
标签的内容不被视为'text'
,因为这些标签不是页面中可见的人类内容的一部分。