Beautiful Soup库的安装
【安装】:
- 使用管理员权限打开cmd
- 输入
pip install beautifulsoup4
3.测试Beautiful Soup 库是否安装完成
示例网站:https://python123.io/ws/demo.html
页面源代码:
(1)使用requests库提取网页
>>> import requests
>>> r=requests.get("https://python123.io/ws/demo.html")
>>> r.status_code
200
>>> r.encoding=r.apparent_encoding
>>> r.text
'<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>'
>>> demo=r.text
>>>
(2)使用Beautiful Soup库
>>> from bs4 import BeautifulSoup #从之前安装的beautifulsoup4中导入BeautifulSoup类
>>> soup=BeautifulSoup(demo,"html.parser") #使用html.parser解释器,对demo进行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>
>>>
Beautiful Soup库的基本元素
1.定义:Beautiful Soup库是解析、遍历、维护“标签树”的功能库
标签:1.名称成对出现
2.属性0个或多个,由键值对构成
【例】:<p class="title">...</p> 名称:p 属性:class="title"
2.引用Beautiful Soup库的方法
from bs4 import BeautifulSoup
注意 BeautifulSoup中,字母“B”和字母“S”要大写
或者:
import bs4
3.调用Beautiful Soup库的格式
soup=BeautifulSoup("<html>data</html>","html.parser")
soup2=BeautifulSoup(open("D://demo.html"),"html.parser")
4.Beautiful Soup的解析器
4.Beautiful Soup的基本元素
>>> from bs4 import BeautifulSoup #获得tag标签的方法
>>> import requests
>>> r=requests.get("https://python123.io/ws/demo.html")
>>> r.status_code
200
>>> r.encoding=r.apparent_encoding
>>> demo=r.text
>>> soup=BeautifulSoup(demo,"html.parser")
>>> soup.title #获取title标签,即页面在浏览器标签页上显示的信息
<title>This is a python demo page</title>
>>> tag=soup.a
>>> tag #打印a标签,即链接标签
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.name #获得a标签的名字
'a'
>>> soup.a.parent.name #获得a标签的父亲标签的名字
'p'
>>> soup.a.parent.parent.name
'body'
>>> tag.attrs #获得标签的属性信息
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> tag.attrs['class'] #获得'class'标签的属性
['py1']
>>> tag.attrs['href'] #获得a标签的链接属性
'http://www.icourse163.org/course/BIT-268001'
>>> type(tag.attrs) #标签属性的类型
<class 'dict'>
>>> type(tag)
<class 'bs4.element.Tag'>
来源:CSDN
作者:luli_ya
链接:https://blog.csdn.net/luli_ya/article/details/104105263