1. 安装BeautifulSoup
由于 BeautifulSoup 库不是 Python 标准库,因此需要单独安装。我们将使用BeautifulSoup 4 版本(也叫 BS4)。
安装方法:$sudo apt-get install python-bs4(python默认是2.x,python3自带BeautifulSoup)
测试是否成功:$python(是python2.x)
> from bs4 import BeautifulSoup
如果没有错误,说明导入成功了。
退出python命令行:>>>quit()
2. 运行BeautifulSoup
创建.py文件:vim xc.py
向.py文件输入: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)
运行:python3 xc.py (正常输出<h1>An Interesting Title</h1>)
python xc.py(输出错误ImportError: No module named request)
更改为:import urllib
from bs4 import BeautifulSoup
html = urllib.urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj.h1)
注意:1. python2 urllib库中没有request模块
2. python3 urllib和urllib2合并为urllib
以下均用python3