Beautiful Soup 库
Beautiful Soup 的使用。
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<p>data</p>',"html.parser")
'''parser为解析器。具体啥意思不知道。。。'''
实际操作:
>>> import requests
>>> r = requests.get("http://python123.io/ws/demo.html")
>>> demo = r.text
>>> from bs4 import BeautifulSoup
>>> 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>
'''
bs4库是解析、遍历、维护“标签树”的功能库。
标签: <p class="title">...</p>
第一个p是这个标签的名称Name,在最开始和最后成对出现,表示范围。
中间有0个或多个属性Attributes,属性是由键值对构成的。
通过bs4库,将html文档(与标签树一一对应)的标签树(字符串)转化成BeautifulSoup类。html-标签树-bs类一一对应。
Beautiful Soup库解析器:
解析器 | 使用方法 | 条件 |
---|---|---|
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 |
Beautiful Soup类的基本元素:
基本元素 | 说明 |
---|---|
Tag | 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾 |
Name | 标签的名字,<p>...</p> 的名字是’p’,格式:<tag>.name |
Attributes | 标签的属性,字典形式组织,格式:<tag>.attrs |
NavigableString | 标签内非属性字符串,<>…</>中字符串,格式:<tag>.string |
Comment | 标签内字符串的注释部分,一种特殊的Comment类型 |
获得tag标签及其name、attrs等的方法:
>>> import requests
>>> r = requests.get("http://python123.io/ws/demo.html")
>>> demo = r.text
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.title
'''<title>This is a python demo page</title>
title标签就是页面在浏览器左上方显示信息的内容。'''
>>> tag = soup.a
>>> tag
'''
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
a标签,就是显示链接的标签。在文本中若存在多个标签,则只返回第一个标签。
'''
#name元素
>>> soup.a.name
'a'
>>> soup.a.parent.name
'p'
>>> soup.a.parent.parent.name
'body'
'''
用一个标签.name的形式获得这个标签的名字。(原来soup.a是一个标签啊。。。)
'''
#attrs元素
>>> tag.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
'''
结果是字典。所以可以进行操作获得相应信息
'''
>>> tag.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> tag.attrs['class'] #因为是字典,所以可以进行相应的操作。
['py1']
>>> tag.attrs['href']
'http://www.icourse163.org/course/BIT-268001'
>>> type(tag.attrs)
<class 'dict'>
>>> type(tag)
<class 'bs4.element.Tag'>
#NavigableString元素
>>> 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.p.string)
<class 'bs4.element.NavigableString'>
#Comment元素
>>> newsoup = BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</P>","html.parser")
>>> newsoup.b.string
'This is a comment'
>>> type(newsoup.b.string)
<class 'bs4.element.Comment'>
>>> newsoup.p.string
'This is not a comment'
>>> type(newsoup.p.string)
<class 'bs4.element.NavigableString'>
>>> type(newsoup)
<class 'bs4.BeautifulSoup'>
>>> type(newsoup.b)
<class 'bs4.element.Tag'>
'''
注意这几个元素type之间的区别。课堂里没有讲但我觉得comment就是在标签后面有个叹号然后两个-表示?我猜。
'''
标签树的遍历:
- 下行遍历:
属性 | 说明 |
---|---|
.contents | 子节点的列表,讲<tag> 所有子节点存入列表 |
.children | 子节点的迭代类型,与.centens类似,用于循环遍历子节点 |
.descendants | 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历 |
>>> import requests
>>> r = requests.get("http://python123.io/ws/demo.html")
>>> demo = r.text
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.head
<head><title>This is a python demo page</title></head>
>>> type(soup.head.contents)
<class 'list'> #contents的类型为list
>>> 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']
#对于一个标签的子节点不仅仅包括标签节点,还包括字符串节点。比如\n也是标签的子节点。
除了contents还可以用children和descendants进行对子孙节点的遍历。
for child in soup.body.children:
print(child)
或者:
for i in soup.body.descendants:
print(i)
- 上行遍历:
属性 | 说明 |
---|---|
.parent | 节点的父亲标签 |
.parents | 节点先辈标签的迭代类型,用于循环遍历先辈节点 |
soup.title.parent
<head><title>This is a python demo page</title></head>
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文本顺序的前续所有平行节点标签 |
只有同一个父亲节点下的节点才有可能是平行节点。
>>> soup.a.next_sibling
' and '
'''注意:字符串也是标签节点,一个标签的下个节点可能是avigableString元素。这里的'and'就是这种情况'''
同样,可以遍历前后续所有平行节点:
for sibling in soup.a.next_siblings:
print(sibling)
基于bs4库的HTML格式化和编码
prittify让html更好看。
>>> demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\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:\r\n<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>\r\n</body></html>'
比较:prettify之后,每个标签后面都有个\n,表示换行。然后就编得好看一点。。。。。(但还是挺丑的,,,这个缩进一点都不明显,看不出来哪个哪个平行啥的。)
>>> soup.prettify()
'<html>\n <head>\n <title>\n This is a python demo page\n </title>\n </head>\n <body>\n <p class="title">\n <b>\n The demo python introduces several python courses.\n </b>\n </p>\n <p class="course">\n Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">\n Basic Python\n </a>\n and\n <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">\n Advanced Python\n </a>\n .\n </p>\n </body>\n</html>'
>>> 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库将所有读入的东西都抓化成utf-8编码。python3系列毫无障碍。。。
上节课一个问题得到了结局,“找不到图片属性”,是由于浏览器问题,换成Chrome浏览器就无障碍了。
这节课的新问题:Comment元素是啥?没咋动。。