BeautifulSoup库的安装及测试

  • 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
BeautifulSoup库的安装及测试_第1张图片

  • 2、测试安装结果
>>> import requests
>>> r=requests.get("http://python123.io/ws/demo.html")
>>> r.text
'This is a python demo page\r\n\r\n

The demo python introduces several python courses.

\r\n

Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\nBasic Python and Advanced Python.

\r\n' >>> demo =r.text >>> from bs4 import BeautifulSoup #bs4是beautifulsoup4库的简写,这里是在bs4 库里面导入一个BeautifulSoup类 >>> soup=BeautifulSoup(demo,"html.parser") #html.parser解析器用于解析demo的html代码 >>> print(soup.prettify()) This is a python demo page

The demo python introduces several python courses.

Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses: Basic Python and Advanced Python .

>>>

你可能感兴趣的:(python爬虫)