python初尝试,写一个简单的爬虫程序

1、首先因为mac自带python,版本为python2.7.10

2、安装pip,因为已经有了,所以不能用brew install。这里用sudo easy_install pip

3、安装beatifulsoup4,sudo -H pip install beautifulsoup4。 BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.

4、安装html5lib,sudo pip install html5lib。html5lib是一种html解析库,其解析方式与浏览器一样

5、脚本代码:  

from urllib2 import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.baidu.com")
res = BeautifulSoup(html.read(),"html5lib")
print(res.title)

6、结果:

以上就是今天的一个小尝试,记录一下。 学习起来也比较有动力~

原文地址:https://www.cnblogs.com/gaofengfengfeng/p/8183137.html