4.1Beautiful Soup库的小测
import requests
r=requests.get('https://python123.io/ws/demo.html')
demo=r.text
print(r.text)
# <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>
from bs4 import BeautifulSoup
soup=BeautifulSoup(demo,"html.parser")#给出的解释器是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>
#解析成功
4.2 Beautiful Soup库的基本元素
4.2.1Beautiful Soup库的理解
Beautiful Soup库是解析、遍历、维护“标签树”的功能库
只要提供的文件是标签类型,Beautiful Soup库都可以对其有很好的解析
每个标签的具体格式
p标签
- 属性定义标签的特点,属性由键值对构成
4.2.2Beautiful Soup库的引用
from bs4 import BeautifulSoup
4.2.3 Beautiful Soup类
Beautiful Soup对应一个HTML/XML文档的全部内容
4.2.4Beautiful Soup类的基本元素
基本元素 | 说明 |
---|---|
Tag | 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾 |
Name | 标签的名字,< p >…</>的名字是‘p’,格式:< tag >.name |
Attributes | 标签的属性,字典形式组织,格式为:< tag>.attrs |
NavigableString | 标签内的非属性字符串,<>…< />中字符串,格式为:< tag>.string |
Comment | 标签内字符串的注释部分,一种特殊的Comment类型 |
from bs4 import BeautifulSoup
import requests
r=requests.get('https://python123.io/ws/demo.html')
demo=r.text
soup=BeautifulSoup(demo,"html.parser")
print(soup)
# <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>
print(soup.title)
#<title>This is a python demo page</title>
tag=soup.a
print(tag)
# <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
print(soup.a.name)#a
print(soup.a.parent.name)#p
print(tag.attrs)#{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
print(tag.attrs['class'])#['py1']
#a标签的链接属性
print(tag.attrs['href'])
#http://www.icourse163.org/course/BIT-268001
#标签属性的类型
print(type(tag.attrs))#<class 'dict'>
#标签类型
print(type(tag))#<class 'bs4.element.Tag'>
#查看a标签
print(soup.a)
# <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
#查看a标签的字符串
print(soup.a.string)#Basic Python
print(soup.p.string)#The demo python introduces several python courses.
print(type(soup.p.string))#<class 'bs4.element.NavigableString'>
newsoup=BeautifulSoup("<b><!--This is a comment--></b><p> This is not a comment</p>","html.parser")
print(newsoup.b.string)#This is a comment
print(type(newsoup.b.string))
#<class 'bs4.element.Comment'>
print(newsoup.p.string)
#This is not a comment
print(type(newsoup.p.string))
#<class 'bs4.element.NavigableString'>
4.3 基于bs4库的HTML内容遍历方法
换一种表示:
4.3.1标签树的下行遍历
属性 | 说明 |
---|---|
.contents | 子节点的列表,将< tag >所有儿子节点存入列表中(只能获取下一级儿子节点) |
.children | 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点 |
.descendants | 子孙节点的迭代类型,包含所有子孙结点,用于循环遍历(可以获取所有子节点) |
print(soup.head)#打印head标签
print(soup.body.contents)#打印body标签的下一级标签
print(len(soup.body.contents))#打印body标签的下一级标签的个数
import requests
r=requests.get('https://python123.io/ws/demo.html')
demo=r.text
from bs4 import BeautifulSoup
soup=BeautifulSoup(demo,"html.parser")#给出的解释器是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>
print(soup.head)
#<head><title>This is a python demo page</title></head>
print(soup.head.contents)
#[<title>This is a python demo page</title>]
print(soup.body.contents)
# ['\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']
print(len(soup.body.contents))#5
print(soup.body.contents[1])
#<p class="title"><b>The demo python introduces several python courses.</b></p>
- 遍历儿子子孙节点
#遍历儿子节点
for child in soup.body.children:
print(child)
#遍历子孙节点
for child in soup.body.children:
print(child)
4.3.2 标签树的上行遍历
属性 | 说明 |
---|---|
.parent | 节点的父亲标签 |
.parents | 节点先辈标签的迭代类型,用于循环遍历先辈节点 |
print(soup.title.parent)
#<head><title>This is a python demo page</title></head>
print(soup.html.parent)
# <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>
for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
# p
# body
# html
# [document]
4.3.3标签树的平行遍历
属性 | 说明 |
---|---|
.next_sibling | 返回按照HTML文本顺序的下一个平行节点标签 |
.previos_sibling | 返回按照HTML文本顺序的上一个平行节点标签 |
.next_siblings | 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签 |
.previous_siblings | 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签 |
平行遍历发生在同一个父节点下的各节点间
print(soup.a.next_sibling)#平行节点的后一个兄弟标签
#and
print(soup.a.previous_sibling)#平行节点的前一个兄弟标签
#Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
for sibling in soup.a.next_siblings:
print(sibling)#循环遍历后续节点
# <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
for sibling in soup.a.previous_siblings:
print(sibling)#循环遍历前续节点
#Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
4.4 基于bs4库的HTML格式化和编码
如何让< HTML>内容更加友好点??
bs4库的prettify()方法
import requests
r=requests.get('https://python123.io/ws/demo.html')
demo=r.text
from bs4 import BeautifulSoup
soup=BeautifulSoup(demo,"html.parser")#给出的解释器是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>
bs4库将任何读入HTML文件和字符串都转化成“utf-8”编码,“utf-8”编码使用国际通用的标准编码格式,能很好支持中文等第三国语言