python库——BeautifulSoup

beautifulsoup可以将html内容解析为soup文档。将不具备良好html格式的网页转化为完整的html文档。
究竟什么是html的完整格式?那么在此之前先介绍一下html。

HTML

HTML是一种超文本标记语言,并不是编程语言。它常与CSS、JavaScript一起用于设计网页、网页应用程序以及移动应用程序的用户界面。

  • 标签
    标签是html的重要成分,通常成对的出现,两个标签之间为元素的内容。
<html>
  <head>
    <title>This is a title</title>
  </head>
  <body>
    <p>Hello world!</p>
  </body>
</html>
<html>和</html>之间的文本描述网页,<body>和</body>之间的文本为可视页面内容。
头部<head>...</head>包含标题。
标记文本<title>This is a title</title>定义了浏览器的页面标题。
标题分为<h1>到<h6>六级,字体依次由大到小。
段落写在<p>...</p>中。
<br>换行。
<a>创建链接。
<a href="https://zh.wikipedia.org/">中文維基百科的連結!</a>

href属性包含链接的url地址。

  • 属性
    了解html的属性对python爬虫有重要意义。
    1.id : id是元素在全文档的唯一标识,用于识别元素。
    2.class : class属性提供一种将类似元素分类的方式。
    3.style : style将可以表现的性质赋给一个特定的元素。
    4.title : title属性给元素一个附加说明。
    5.lang : lang用于识别元素内容的语言。

例:

<abbr id="ID" class="术语" style="color:purple;" title="超文本标记语言">HTML</abbr>

abbr为缩写元素。

BeautifulSoup的使用

beautifulsoup可以将不良html格式的网页解析为完整的html文档,并能按照标准的缩进格式的结构输出

>>>from bs4 import BeautifulSoup
>>>broken_html = '<ul class=shop><li>Price<li>Number</ul>'
>>>#解析此html
>>>soup = BeautifulSoup(broken_html,'html.parser'>)
>>>fixed_html = soup.prettify()
>>>print(fixed_html)
<ul class="shop">
 <li>
  Price
  <li>
   Number
  </li>
 </li>
</ul>

以一段html文档来举例说明它的使用方法:

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = BeautifulSoup(html_doc)
print(soup.prettify())
# <html>
#  <head>
#   <title>
#    The Dormouse's story
#   </title>
#  </head>
#  <body>
#   <p class="title">
#    <b>
#     The Dormouse's story
#    </b>
#   </p>
#   <p class="story">
#    Once upon a time there were three little sisters; and their names were
#    <a class="sister" href="http://example.com/elsie" id="link1">
#     Elsie
#    </a>
#    ,
#    <a class="sister" href="http://example.com/lacie" id="link2">
#     Lacie
#    </a>
#    and
#    <a class="sister" href="http://example.com/tillie" id="link2">
#     Tillie
#    </a>
#    ; and they lived at the bottom of a well.
#   </p>
#   <p class="story">
#    ...
#   </p>
#  </body>
# </html>

soup.title
# <title>The Dormouse's story</title>
soup.title.string
#"The Dormouse's story"
soup.title.parent.name
#'head'
soup.p
#<p class="title"><b>The Dormouse's story</b></p>
soup.find_all('a')
#[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.find(id="link3")
#<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

tag对象与html中tag对象属性相同

soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
tag.name
# 'b'
#修改tag名称可以修改生成的html文档,tag的属性可以被添加,删除或修改
tag.name = "blockquote"
tag
#<blockquote class="boldest">Extremely bold</blockquote>
tag['id'] = 1
tag
#<blockquote class="boldest" id="1">Extremely bold</blockquote>

由于BeautifulSoup模块是纯python编写而正则模块是C语言编写的,与正则表达式相比BeautifulSoup抓取速度要慢很多,但其语法比正则表达式要简单易懂的多,上手简单,推荐新手使用。

参考文献:html维基百科: https://zh.wikipedia.org/wiki/HTML
BeautifulSoup4.2.0文档 : https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶柖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值