1. 输出
1.1 格式化输出
- prettify() 方法将 Beautiful Soup 的文档树格式化后以 Unicode 编码输出;
- 每个 XML/HTML 标签都独占一行;
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
print(soup.prettify())
输出:
<html>
<body>
<a href="http://example.com/">
I linked to
<i>
example.com
</i>
</a>
</body>
</html>
- BeautifulSoup 对象和它的 tag 节点都可以调用 prettify() 方法。
1.2 压缩输出
- unicode() 或 str() 方法:
- 只得到结果字符串,不重视格式;
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
print(str(soup))
print(unicode(soup.a))
输出:
<html><head></head><body><a href="http://example.com/">I linked to <i>example.com</i></a></body></html>
<a href="http://example.com/">I linked to <i>example.com</i></a>
- str() 方法返回 UTF-8 编码的字符串,可以指定编码的设置。
- encode() 方法获得字节码或调用 decode() 方法获得Unicode。
1.3 输出格式
- Beautiful Soup 输出是会将 HTML 中的特殊字符转换成 Unicode,比如
“&lquot;”
;
soup = BeautifulSoup("“Dammit!” he said.")
print(unicode(soup))
输出:
<html><head></head><body>\u201cDammit!\u201d he said.</body></html>
- 如果将文档转换成字符串,Unicode 编码会被编码成 UTF-8;
- 这样就无法正确显示 HTML 特殊字符了;
soup = BeautifulSoup("“Dammit!” he said.")
print(str(soup))
输出:
<html><head></head><body>\xe2\x80\x9cDammit!\xe2\x80\x9d he said.</body></html>
1.4 get_text()
- get_text() 方法:得到 tag 中包含的文本内容;
- 这个方法获取到 tag 中包含的所有文版内容包括子孙 tag 中的内容,并将结果作为 Unicode 字符串返回;
markup = '<a href="http://example.com/">\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup)
print(soup.get_text())
print(soup.i.get_text())
输出:
'\nI linked to example.com\n'
example.com
- 可以通过参数指定 tag 的文本内容的分隔符:
print(soup.get_text("|"))
输出:
\nI linked to |example.com|\n
- 去除获得文本内容的前后空白:
print(soup.get_text("|", strip=True))
输出:
I linked to|example.com