Beautifulsoup 库 -- 05 -- 输出

本文介绍了BeautifulSoup库中关于输出的三个关键点:格式化输出的prettify()方法,压缩输出的str()和unicode(),以及get_text()用于提取文本内容。通过实例演示了如何优雅地呈现HTML结构和提取所需信息。
摘要由CSDN通过智能技术生成

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("&ldquo;Dammit!&rdquo; he said.")
print(unicode(soup))

输出:

<html><head></head><body>\u201cDammit!\u201d he said.</body></html>
  • 如果将文档转换成字符串,Unicode 编码会被编码成 UTF-8;
  • 这样就无法正确显示 HTML 特殊字符了;
soup = BeautifulSoup("&ldquo;Dammit!&rdquo; 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值