Python爬虫之bs4库

                                      Python爬虫之bs4库

一、BeautifulSoup的基本使用

     1、使用BeautifulSoup解析,返回一个BeautifulSoup的对象,并按照标准的缩进格式结构化输出:

from bs4 import BeautifulSoup
htmlDoc="""
<table class='citytable'>
<tr class='citytr'><td><a href='43/4305.html'>430500000000</a></td>
<td><a href='43/4305.html'>邵阳市</a></td></tr></table>
"""
soup=BeautifulSoup(html,"lxml")  # 解析网页文档
print(soup.prettify()) # 按照标准的缩进格式结构化输出

    运行结果:

<html>
 <body>
  <table class="citytable">
   <tr class="citytr">
    <td>
     <a href="43/4305.html">
      430500000000
     </a>
    </td>
    <td>
     <a href="43/4305.html">
      邵阳市
     </a>
    </td>
   </tr>
  </table>
 </body>
</html>

   注:解析后自动添加了html、body标签

   2、解析器

解析器

使用方法

优势

劣势

Python标准库

BeautifulSoup(markup, "html.parser")

Python的内置标准库执行速度适中文档容错能力强

Python 2.7.3 or 3.2.2)前的版本中文档容错能力差

lxml HTML 解析器

BeautifulSoup(markup, "lxml")

速度快文档容错能力强

需要安装C语言库

lxml XML 解析器

BeautifulSoup(markup, ["lxml-xml"])

BeautifulSoup(markup, "xml")

速度快

唯一支持XML的解析器

需要安装C语言库

html5lib

BeautifulSoup(markup, "html5lib")

最好的容错性

以浏览器的方式解析文档

生成HTML5格式的文档

速度慢

不依赖外部扩展

二、BeautifulSoup对象的种类

   Beautiful Soup将HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种Tag , NavigableString , BeautifulSoup , Comment 。

     1、Tag对象

    ①Tag的Name属性

from bs4 import BeautifulSoup
htmlDoc="""
<table class='citytable'>
<tr class='citytr'><td><a href='43/4305.html'>430500000000</a></td>
<td><a href='43/4305.html'>邵阳市</a></td></tr></table>
"""
soup=BeautifulSoup(htmlDoc,"lxml")  # 解析网页文档
tag=soup.tr   # tag对象
print(type(tag),"\n"+"-"*70)
print(tag.name,"\n"+"-"*70) # tag name属性即标签的名字
tag.name="ee"   # 修改name,会影响原网页文档
print(soup,"\n"+"-"*70) # 打印htmlDoc

    返回结果:

<class 'bs4.element.Tag'> 
----------------------------------------------------------------------
tr 
----------------------------------------------------------------------
<html><body><table class="citytable">
<ee class="citytr"><td><a href="43/4305.html">430500000000</a></td>
<td><a href="43/4305.html">邵阳市</a></td></ee></table>
</body></html> 
----------------------------------------------------------------------

    ②Tag的Attributes属性

    Tag的Attributes即标签的属性,Tag既可能存在多个属性也可能存在多个值。

from bs4 import BeautifulSoup
htmlDoc="""
<table class='citytable'>
<tr class='city town' id="1234"><td><a href='43/4305.html'>430500000000</a></td>
<td><a href='43/4305.html'>邵阳市</a></td></tr></table>
"""
soup=BeautifulSoup(htmlDoc,"lxml")  # 解析网页文档
tag=soup.a  # tag对象
print(tag["href"],"\n"+"-"*70)  # tag属性的值即标签属性的值
print(soup.tr["class"],"\n"+"-"*70)  # tr的class属性
print(tag.attrs,"\n"+"-"*70) # tag的属性和值
tag=soup.tr # tag对象
print(tag.attrs,"\n"+"-"*70) # tag属性的值即标签属性的值
del tag["id"] # 删除tag的id属性
# print(tag["id"]) 不存在的Tag属性会报错
print(tag.get("id"),"\n"+"-"*70) # 而tag的get方法获取不存在的属性不会报错
tag["class"]="city"  # 修改tag的属性的值
print(soup,"\n"+"-"*70) # 打印解析后的网页文档

    返回结果:

43/4305.html 
----------------------------------------------------------------------
['city', 'town'] 
----------------------------------------------------------------------
{'href': '43/4305.html'} 
----------------------------------------------------------------------
{'class': ['city', 'town'], 'id': '1234'} 
----------------------------------------------------------------------
None 
----------------------------------------------------------------------
<html><body><table class="citytable">
<tr class="city"><td><a href="43/4305.html">430500000000</a></td>
<td><a href="43/4305.html">邵阳市</a></td></tr></table>
</body></html> 
----------------------------------------------------------------------

    对于任何版本的HTML定义中都没有被定义为多值属性,那么Beautiful Soup会将这个属性作为字符串返回

from bs4 import BeautifulSoup
htmlDoc="""
<table class='citytable'>
<tr class='city town' id="12 34"><td><a href='43/4305.html'>430500000000</a></td>
<td><a href='43/4305.html'>邵阳市</a></td></tr></table>
"""
soup=BeautifulSoup(htmlDoc,"lxml")  # 解析网页文档
tag=soup.tr  # tag对象
print(tag["id"])

    返回结果:

12 34

    如果转换的文档是XML格式,那么tag中不包含多值属性:

from bs4 import BeautifulSoup
htmlDoc="""
<table class='citytable'>
<tr class='city town' id="1234"><td><a href='43/4305.html'>430500000000</a></td>
<td><a href='43/4305.html'>邵阳市</a></td></tr></table>
"""
soup=BeautifulSoup(htmlDoc,"xml")  # 解析网页文档
tag=soup.tr  # tag对象
print(tag["class"])

    返回结果:

city town

猜你喜欢

转载自blog.csdn.net/weixin_41859405/article/details/81511613