Python中的Beautiful Soup模块的用法

发布时间:2021-09-04 10:11:36 作者:chen
来源:亿速云 阅读:127

这篇文章主要介绍“Python中的Beautiful Soup模块的用法”,在日常操作中,相信很多人在Python中的Beautiful Soup模块的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python中的Beautiful Soup模块的用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1.Beautiful Soup模块的介绍

pip install beautifulsoup4
pip install html5lib
pip install lxml

2. Beautiful Soup模块解析HTML文档

假如现在有一段不完整的HTML代码,我们现在要使用Beautiful Soup模块来解析这段HTML代码

data = '''                                         
<html><head><title>The Dormouse's story</title></he
<body>                                             
<p class="title"><b id="title">The Dormouse's story</b></p>   
<p class="story">Once upon a time there were three 
<a href="http://example.com/elsie" class="sister" i
<a href="http://example.com/lacie" class="sister" i
<a href="http://example.com/tillie" class="sister" 
and they lived at the bottom of a well.</p>        
<p class="story">...</p>                           
'''
from bs4 import BeautifulSoup           
soup = BeautifulSoup(data,'lxml')

然后通过BeautifulSoup提供的方法就可以拿到HTML的元素、属性、链接、文本等,BeautifulSoup模块可以将不完整的HTML文档,格式化为完整的HTML文档 ,比如我们打印print(soup.prettify())看一下输出什么?

<html>
 <head>
  <title>
   The Dormouse's story
  </title>
 </head>
 <body>
  <p class="title">
   <b id="title">
    The Dormouse's story
   </b>
  </p>
  <p class="story">
   Once upon a time there were three
   <a a="" and="" at="" bottom="" class="sister" href="http://example.com/elsie" i="" lived="" of="" the="" they="" well.="">
    <p class="story">
     ...
    </p>
   </a>
  </p>
 </body>
</html>
print('title = {}'.format(soup.title))             
# 输出:title = <title>The Dormouse's story</title>
print('a={}'.format(soup.a))
print('title_name = {}'.format(soup.title.name))
# 输出:title_name = title
print('body_name = {}'.format(soup.body.name))
# 输出:body_name = body
print('title_string = {}'.format(soup.title.string))
#  输出:title_string = The Dormouse's story
print('title_pareat_name = {}'.format(soup.title.parent))
# 输出:title_pareat_name = <head><title>The Dormouse's story</title>
</head>
print('p = {}'.format(soup.p))
# 输出:p = <p class="title"><b>The Dormouse's story</b></p>
print('p_class = {}'.format(soup.p["class"]))
# 输出:p_class = ['title']
print('a_class = {}'.format(soup.a["class"]))
# 输出:a_class = ['sister']
#  获取所有的a标签
print('a = {}'.format(soup.find_all('a')))
#  获取所有的p标签  
print('p = {}'.format(soup.find_all('p')))
print('a_link = {}'.format(soup.find(id='title')))
# 输出:a_link = <b id="title">The Dormouse's story</b>

3.BeautifulSoup中的对象

语法:

4.遍历文档

soup = BeautifulSoup(html,"lxml")
# 返回一个列表
print(soup.p.contents)
# 拿到第一个子节点
print(soup.p.contents[0])
for tag in soup.p.children:
    print(tag)
soup = BeautifulSoup(html,"lxml")
for content in soup.strings:
    print(repr(content))
soup = BeautifulSoup(html,"lxml")
for tag in soup.stripped_strings:
    print(repr(tag))

5.查找标签

soup = BeautifulSoup(html,"lxml")
print(soup.find_all('a'))
print(soup.find_all(['a','p']))
print(soup.find_all(re.compile('^a')))
soup = BeautifulSoup(html,"lxml")
print(soup.find('a'))

到此,关于“Python中的Beautiful Soup模块的用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 爬虫入门之Beautiful Soup
  2. python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:oracle全局临时表有的两种模式介绍

下一篇:MySQL中的隐藏列的具体查看方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》