
BeautifulSoup安装及其使用
BeautifulSoup是个好东东。
官网见这里:
下载地址见这里:,附件有4.1.2的安装源码
文档见这里:,是中文翻译的,不过文档有点旧,是3.0的文档版本,看起来没有什么意思。
我推荐大家看个:,这个是python的官网英文版,看起来要舒服,清晰很多。
在python下,你想按照jquery格式来读取网页,免除网页格式、标签的不规范的困扰,那么BeautifulSoup是个不错的选择。按照官网所说,BeautifulSoup是Screen-Scraping应用,旨在节省大家处理HTML标签,并且从网络中获得信息的工程。BeautifulSoup有这么几个优点,使得其功能尤其强大:
1:Beautiful Soup provides a few methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. It doesn't take much code to write an application。关键词:python风格、提供简单方法
2:Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don't have to think about encodings, unless the document doesn't specify an encoding and Beautiful Soup can't autodetect one. Then you just have to specify the original encoding。关键词:编码转换,使用Python的同学都会认同Python编码格式的繁琐,BeautifulSoup能简化这一点。
3:Beautiful Soup sits on top of popular Python parsers likelxmlandhtml5lib, allowing you to try out different parsing strategies or trade speed for flexibility。关键词:兼容其它html解析器,能够让你随心替换。
看完这几个特性,想必有人心动了吧,我们先看下BeautifulSoup的安装:
安装方法:
1:apt-get install python-bs4
2:easy_install beautifulsoup4
3:pip install beautifulsoup4
4:源码安装:python setup.py install
根据不同的操作系统,选用不同的安装方法,这些方法都能安装成功,不同点在于安装的工具不同。我自己的系统采用的是第四种安装方法,下面我来简要介绍下第四种安装方法:
Python代码
curl>>beautifulsoup4-4.1.2.tar.gz
tarzxvfbeautifulsoup4-4.1.2.tar.gz
cdbeautifulsoup4-4.1.2
pythonsetup.pyinstall
Ok,你就能看到安装信息,提示安装成功。
安装成功,肯定想迫不及待的使用,你打开python command窗口,你很happy的输入:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-29971-1.html
就是以鬼城为平台