Python Beautiful Soup库简单讲解--包括安装

Beautiful Soup库入门

本文是Mooc上 Python网络爬虫与信息提取 的笔记

1 Beautiful Soup库的安装

这里以PyCharm为例(因为我用的就是这种方法)

点击的顺序为

File->Settings->Project:Pycharm->Python Interperter->

然后点击右上角的加号.添加我们想要加入的库

2 Beautiful Soup库的基本元素

一个HTML文件是由一组<>构成的标签组成的

Beautiful Soup库就是用来处理这些标签的

<p>..</p> : 标签  Tag

<p class=“title”> … </p>

名称  Name   属性  Attributes
成对出现			0个或者多个

Beautiful Soup库的引用

Beautiful Soup库,也叫 beautifulsoup4或者bs4

引用方式如下

 from bs4 import BeautifulSoup

HTML文档 标签树 BeautifulSoup 是相等的

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

BeautifulSoup库的解析器

soup = BeautifulSoup(‘data’,‘html.parser’)

‘html.parser’ 就是更改的内容

解析器使用方法条件
bs4的HTML解析器BeautifulSoup(mk,‘html.parser’)安装bs4库
lxml的HTML解析器BeautifulSoup(mk,‘lxml’)pip install lxml
lxml的XML解析器BeautifulSoup(mk,‘xml’)pip install lxml
html5lib的解析器BeautifulSoup(mk,‘html5lib’)pip install html5lib
BeautifulSoup类的基本元素
<p class=“title”> … </p>
基本元素说明
Tag标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾
Name标签的名字,

的名字是’p’,格式:.name
Attributes标签的属性,字典形式组织,格式:.attrs
NavigableString标签内非属性字符串,<>…</>中字符串,格式:.string
Comment标签内字符串的注释部分,一种特殊的Comment类型
一堆例子
Tag的使用
import  requests
from bs4 import BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text   #获取到url 地点的源码
soup = BeautifulSoup(demo,'html.parser')
print(soup.title)	#输出title标签的内容

结果如下

<title>This is a python demo page</title>
查看标签的名字
import  requests
from bs4 import BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text   #获取到url 地点的源码
soup = BeautifulSoup(demo,'html.parser')
print(soup.a.name)                      #输出a标签的名字
print(soup.a.parent.name)               #输出a标签的父标签的名字
print(soup.a.parent.parent.name)        #输出a标签的父标签的父标签的名字

结果如下

a
p
body
标签的属性信息
import  requests
from bs4 import BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text   #获取到url 地点的源码
soup = BeautifulSoup(demo,'html.parser')
tag = soup.a
print(tag.attrs)            #输出a标签的属性
print(tag.attrs['class'])   #输出a标签中的class属性
print(tag.attrs['href'])    #输出a标签的链接属性
print(type(tag.attrs))      #输出a属性的类型
print(type(tag))            #输出标签的类型(返回的是一个字典)

结果如下

{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
['py1']
http://www.icourse163.org/course/BIT-268001
<class 'dict'>
<class 'bs4.element.Tag'>
NavigableString元素的处理
import  requests
from bs4 import BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text   #获取到url 地点的源码
soup = BeautifulSoup(demo,'html.parser')
print(soup.a)
print(soup.a.string)    #查办a标签中的字符串
print(soup.p)
print(soup.p.string)    #查看p标签中的字符串
print(type(soup.p.string))  #查看p标签中的字符串的类型

结果如下

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
Basic Python
<p class="title"><b>The demo python introduces several python courses.</b></p>
The demo python introduces several python courses.
<class 'bs4.element.NavigableString'>
关于注释
import  requests
from bs4 import BeautifulSoup

newsoup = BeautifulSoup('<b><!--This is a comment--></b><p>This is not a comment</p>','html.parser')
print(newsoup.b.string)
print(type(newsoup.b.string))
print(newsoup.p.string)
print(type(newsoup.p.string))

结果如下

This is a comment
<class 'bs4.element.Comment'>
This is not a comment
<class 'bs4.element.NavigableString'>

3 基于bs4库的HTML内容遍历方法

回顾demo的例子

import requests
r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
print(demo)

结果如下:

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

HTML的基本格式可以分为一个树形结构

标签树的下行遍历

BeautifulSoup类型是标签树的根节点

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

学习代码如下

import requests
from  bs4 import  BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
soup = BeautifulSoup(demo,'html.parser')
print(soup.head)
print(soup.head.contents)       #.contents返回的类型是列表
print(soup.body.contents)
print(len(soup.body.contents))  #输出body结点的个数
print(soup.body.contents[1])    #输出body结点的第二个

for child in soup.body.children:    #遍历儿子节点
    print(child)

for child in soup.body.descendants:     #遍历子孙节点
	print(child)

输出结果如下

<head><title>This is a python demo page</title></head>
[<title>This is a python demo page</title>]
['\n', <p class="title"><b>The demo python introduces several python courses.</b></p>, '\n', <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>, '\n']
5
<p class="title"><b>The demo python introduces several python courses.</b></p>


<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>




<p class="title"><b>The demo python introduces several python courses.</b></p>
<b>The demo python introduces several python courses.</b>
The demo python introduces several python courses.


<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>
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>
Basic Python
 and 
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
Advanced Python
.
标签树的上行遍历
属性说明
.parent节点的父亲标签
.parents节点先辈标签的迭代类型,用于循环遍历先辈节点

学习代码

import requests
from  bs4 import  BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
soup = BeautifulSoup(demo,'html.parser')

for parent in soup.a.parents:
    if parent is None:      #他的父亲结点可能是空的,所以要加上这样的一句
        print(parent)
    else:
        print(parent.name)

结果如下

p
body
html
[document]
标签树的平行遍历
属性说明
.next_sibling返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling返回按照HTML文本顺序的上一个平行节点标签
.next_siblings迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous_siblings迭代类型,返回按照HTML文本顺序的前续所有平行节点标签

平行遍历发生再同一个父结点下的各节点间

import requests
from  bs4 import  BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
soup = BeautifulSoup(demo,'html.parser')

for sibling in soup.a.next_siblings:  #遍历后续结点
    print(sibling)

for sibling in soup.a.previous_siblings: #遍历
    print(sibling)

结果如下

 and 
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
.
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:


基于bs4库的HTML格式输出

bs4库的prettify()方法

import requests
from  bs4 import  BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
soup = BeautifulSoup(demo,'html.parser')
soup.prettify()
print(soup.prettify())  #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>

总结:

BeautifulSoup

① 理解Bs4库的基本元素

② 了解遍历的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值