- BeautifulSoup库解释
BeautifulSoup模块是用来从HTML/XML等文件提取所需数据的Python库.,专为快速周转项目而设计,如屏幕抓取。三个功能使其功能强大:
(1)Beautiful Soup提供了一些简单的方法和Pythonic习语,用于导航,搜索和修改解析树:用于剖析文档和提取所需内容的工具包。编写应用程序不需要太多代码
(2)Beautiful Soup会自动将传入的文档转换为Unicode,将传出的文档转换为UTF-8。你不必考虑编码,除非文档未指定编码且Beautiful Soup无法检测到编码。然后你只需要指定原始编码。
(3)Beautiful Soup位于流行的Python解析器之上,如lxml和html5lib,允许尝试不同的解析策略或交易速度以获得灵活性。
- 1、安装beautifulsoup库
以管理员权限运行pip install beautifulsoup4
- 2、测试安装结果
>>> import requests
>>> r=requests.get("http://python123.io/ws/demo.html")
>>> 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
>>> from bs4 import BeautifulSoup #bs4是beautifulsoup4库的简写,这里是在bs4 库里面导入一个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>
>>>