Beautiful Soup输出

Beautiful Soup输出

Beautiful Soup输出

  pretty() 请求库Python编程 的 HTTP 请求到指定的 URL 的组成部分。无论是 REST API 还是 Web Scrapping,都必须学习请求才能进一步使用这些技术。当一个人向一个 URI 发出请求时,它会返回一个响应。Python 请求提供了用于管理请求和响应的内置功能。

BeautifulSoup输出

  上篇文章我们为你介绍 Beautiful Soup搜索文档树,在本章中,我们晓得博客将为你介绍Beautiful Soup输出。

格式化输出

Beautifulsoup格式化输出

  prettify() 方法将Beautiful Soup的文档树格式化后以 Unicode编码输出,每个 XML/HTML 标签都独占一行

import lxml
from bs4 import BeautifulSoup

markup = '<a href="https://www.pythonthree.com/what-is-cms/" target="_blank">内容管理系统</a>(CMS)。' \
         '<br>  WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,'

soup = BeautifulSoup(markup, "lxml")

soup.prettify()

print(soup.prettify())

  输出:

<html>
 <body>
  <a href="https://www.pythonthree.com/what-is-cms/" target="_blank">
   内容管理系统
  </a>
  (CMS)。
  <br/>
  WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,
 </body>
</html>

  BeautifulSoup 对象和它的 tag节点 都可以调用 prettify() 方法:

print(soup.a.prettify())



<a href="https://www.pythonthree.com/what-is-cms/" target="_blank">
 内容管理系统
</a>

压缩输出

Beautifulsoup压缩输出

  如果只想得到结果字符串,不重视格式,那么可以对一个 BeautifulSoup 对象 或 Tag对象使用Python的 unicode() 或 str() 方法:

str(soup)
# '<html><body><a href="https://www.pythonthree.com/what-is-cms/" target="_blank">内容管理系统</a>(CMS)。<br/>  WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,</body></html>'

unicode(soup.a)
# <a href="https://www.pythonthree.com/what-is-cms/" target="_blank">内容管理系统</a>


  str() 方法返回 UTF-8编码 的字符串,可以指定 编码 的设置。还可以调用 encode() 方法获得字节码或调用 decode() 方法获得Unicode.

(soup.a).decode()

#<a href="https://www.pythonthree.com/what-is-cms/" target="_blank">内容管理系统</a>

输出格式

  Beautiful Soup输出是会将 HTML 中的特殊字符转换成 Unicode,比如“ &lquot; ”:

soup = BeautifulSoup("&ldquo;Dammit!&rdquo; he said.")
unicode(soup)
# u'<html><head></head><body>\u201cDammit!\u201d he said.</body></html>'

  如果将文档转换成字符串,Unicode编码会被编码成 UTF-8,这样就无法正确显示 HTML 特殊字符了:

str(soup)
# '<html><head></head><body>\xe2\x80\x9cDammit!\xe2\x80\x9d he said.</body></html>'

  推荐:使用BeautifulSoup查询关键词谷歌搜索结果排名

get_text()

  如果只想得到 tag 中包含的文本内容,那么可以用 get_text() 方法,这个方法获取到 tag 中包含的所有文版内容包括 子孙tag 中的内容,并将结果作为Unicode字符串返回:

markup = '<a href="https://www.pythonthree.com/what-is-cms/" target="_blank">内容管理系统</a>(CMS)。' \
         '<br>  WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,'
soup = BeautifulSoup(markup, "lxml")

soup.get_text()


内容管理系统(CMS)。  WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,

  可以通过参数指定 tag 的文本内容的分隔符:

# soup.get_text("|")

内容管理系统丨(CMS)。丨  WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,

  还可以去除获得文本内容的前后空白:

# soup.get_text("|", strip=True)

内容管理系统|(CMS)。|WordPress 适用于各种网站的平台,从个人博客到电子商务网站到商业网站,

  或者使用 .stripped_strings 生成器,获得文本列表后手动处理列表:

总结

  以上是 晓得博客为你介绍的Beautiful Soup输出的全部内容, 希望对你的 BeautifulSoup学习有所帮助,欢迎留言讨论。更多内容可参考官方文档

  推荐:Beautiful Soup 教程

5/5 - (2 votes)

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » Beautiful Soup输出

转载请保留链接:https://www.pythonthree.com/beautiful-soup-output/

Claude、Netflix、Midjourney、Chatgpt Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
Kinsta-free-hosting主机
Kinsta-free-hosting主机
Elementor可视化创建WordPress网站
Elementor可视化创建WordPress网站
滚动至顶部