爬虫学习_BeautifulSoup安装使用
2020-03-15 本文已影响0人
leogoforit
BeautifulSoup是python爬虫中常用的库,它通过定位HTML标签来格式化和组织复杂的网络信息,用简单易用的Python对象为我们展现XML结构信息。
这篇文章是我在学习图书《Python 网络数据采集》的笔记。

1、安装BeautifulSoup
现在python3都用的是BeautifulSoup4,由于我使用的是anaconda,所以这些包就不用单独安装了。单独安装的方法为:
pip install BeautifulSoup4
2、爬取一个简单网页的标题
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj.h1)
输出以下结果:
<h1>An Interesting Title</h1>