Python爬虫之Beautiful Soup库的安装与使用

来源:互联网 发布:有赞api php 调用实例 编辑:程序博客网 时间:2024/05/18 01:42

一.Beautiful Soup库的安装

在windows下以管理员身份运行CMD命令行,输入:
pip install beautifulsoup4

二.bs4库的简单使用

打开IDEL,在其中编写如下代码:

#从bs4库中引用BeautifulSoup类from bs4 import BeautifulSoupimport requestsr = requests.get("http://www.baidu.com")r.status_coder.encoding = r.apparent_encodingdemo = r.text#html.parser是HTML和XML文档解析器soup = BeautifulSoup(demo, "html.parser")#输出文档的头部信息soup.head

PS:Beautiful Soup库是解析、遍历、维护标签树的功能库

0 0