BeautifuSoup入门

Beautiful Soup库是一个解析HTML文件的优秀的库。

解析,遍历,维护“标签树”的功能库。

安装:

  pip安装:  pip install beautifulsoup4

  conda安装  conda install beautifulsoup4

导入:

  from bs4 import BeautifulSoup

对BeautifulSoup类的理解:

  一个BeautifulSoup对象对应一个HTML/XML文档的全部内容。

  soup = BeautifulSoup(open('D://demo.html'),'html.parser')
  soup2 = BeautifulSoup('<html>data</html>','html.parser')
  r= requests.get('www.baiidu.com')
  soup3 = BeautifulSoup(r.text,'html.parser')

  对soup对象调用prettify()可以将HTML按格式展示出来,方便阅读

  import requests
  from bs4 import BeautifulSoup
  r = requests.get('https://www.baidu.com/')
  r.encoding = r.apparent_encoding
  demo = r.text
  soup = BeautifulSoup(demo, 'html.parser')
  print(soup.prettify())

  以baidu为例

  打印出来的其实就是baidu的源代码。

  后面跟的 html.parser 是指定用bs4的HTML解析器对网页解析, 可以指定别的解析器

BeautifulSoup类的基本元素:

基本元素 说明
Tag 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾
Name  标签的名字,<p>...</p>的名字是‘p’,格式:<tag>.name
Attributes 标签的属性,字典形式组织,格式:<tag>.attrs
NavigableString 标签内非属性字符串,<>...</>中的字符串,格式:<tag>.string
Comment  标签内字符串的注释部分,一种特殊的Comment类型

  以 http://python123.io/ws/demo.html 为例

>>> r = requests.get('http://python123.io/ws/demo.html')
         
>>> demo=r.text
         
>>> soup = BeautifulSoup(demo, 'html.parser')
         
>>> print(soup.prettify())
         
<html>
 <head>
  <title>
   This is a python demo page
  </title>
 </head>
 <body>
  <p class="title">
   <b>
    The demo python introduces several python courses.
   </b>
  </p>
  <p class="course">
   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
    Basic Python
   </a>
   and
   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>

  Tag标签

>
>>> tag = soup.a
         
>>> tag
         
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.body
         
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body>
>>> 

  Tag的name(返回字符串):

>>> tag.name
         
'a'
>>> soup.body.name
         
'body'
>>> soup.a.parent.name
         
'p'
>>> soup.a.parent.parent.name
         
'body'

  Tag的attrs(返回字典类型,一个tag可以有多个属性):

>>> tag = soup.a
         
>>> tag
         
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> tag.attrs
         
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> tag.attrs['href']
         
'http://www.icourse163.org/course/BIT-268001'

  Tag的NavigableString(可以跨越多个层次,留意p标签)

>>> soup.a
         
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.string
         
'Basic Python'
>>> soup.p
         
<p class="title"><b>The demo python introduces several python courses.</b></p>
>>> soup.p.string
         
'The demo python introduces several python courses.'
>>> type(soup.a.string)
         
<class 'bs4.element.NavigableString'>

  Tag的Comment(注释类,用type区分于NavigableString):

>>> newsoup = BeautifulSoup('<b><!--This is a comment--></b><a>This is not a comment</a>', 'html.parser')
         
>>> newsoup.b.string
         
'This is a comment'
>>> type(newsoup.b.string)
         
<class 'bs4.element.Comment'>
>>> newsoup.a.string
         
'This is not a comment'
>>> type(newsoup.a.string)
         
<class 'bs4.element.NavigableString'>

 标签树的遍历:

  下行遍历

属性 说明
.contents 子节点的列表,将<tag>所有的儿子节点存入列表
.children 子节点的迭代类型,与.content类似,用于循环遍历儿子节点
.descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历

 

  上行遍历

属性 说明
.parent 节点的父亲标签
.parents 节点先辈标签的迭代类型,用于循环遍历先辈节点

 

 

 

  平行遍历

属性 说明
.next_sibling 返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
.next_silblings 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous_siblings 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签

 

 

 

 

 

 

 

bs4库的编码

  bs4库将任何HTML输入都变成utf-8编码

 

基于bs4库的HTML内容查找方法

<>.find_all(name, attrs, recursive, string, **kwargs)
    • name  :对标签名称的检索字符串
    • attrs    :对标签属性值的检索字符串,可标注属性检索
    • recursive  :是否对子孙全部检索,默认True 
    • string   :<>...</>中字符穿区域的检索字符串

  <tag>(..)    等价与 <tag>.fing_all(..)

  soup(..)   等价于 soup.find_all(..)

扩展方法

方法 说明
<>.find()

搜索且只返回一个结果,同.find_all()参数

<>.find_parents()

在先辈节点中搜索,返回列表类型,同.find_all()参数
<>.find_parent()

在先辈节点中返回一个结果,同.find()参数

<>.find_next_siblings()

在后续平行节点中搜索,返回列表类型,同.find_all()参数

<>.find_next_sibling()

在后续平行节点中返回一个结果,同.find()参数

<>.find_previous_siblings()

在前序平行节点中搜索,返回列表类型,同.find_all()参数

<>.find_previous_sibling() 在前序平行节点中返回一个结果,同.find()参数
原文地址:https://www.cnblogs.com/hao11/p/12727497.html