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>
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>
“&lquot;”
;soup = BeautifulSoup("“Dammit!” he said.")
print(unicode(soup))
输出:
<html><head></head><body>\u201cDammit!\u201d he said.</body></html>
soup = BeautifulSoup("“Dammit!” he said.")
print(str(soup))
输出:
<html><head></head><body>\xe2\x80\x9cDammit!\xe2\x80\x9d he said.</body></html>
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
print(soup.get_text("|"))
输出:
\nI linked to |example.com|\n
print(soup.get_text("|", strip=True))
输出:
I linked to|example.com
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/S_numb/article/details/120218236
内容来源于网络,如有侵权,请联系作者删除!