奥迪汽车建设网站百度销售系统登录
奥迪汽车建设网站,百度销售系统登录,凡科可以做社交网站吗,营销型网站建设明细From: http://www.crifan.com/python_third_party_lib_html_parser_beautifulsoup/ 背景
在Python去写爬虫#xff0c;网页解析等过程中#xff0c;比如#xff1a;
如何用Python#xff0c;C#等语言去实现抓取静态网页抓取动态网页模拟登陆网站
常常需要涉及到HTML等网…From: http://www.crifan.com/python_third_party_lib_html_parser_beautifulsoup/ 背景
在Python去写爬虫网页解析等过程中比如
如何用PythonC#等语言去实现抓取静态网页抓取动态网页模拟登陆网站
常常需要涉及到HTML等网页的解析。
当然对于简单的HTML中内容的提取Python内置的正则表达式Re模块就足够用了
但是对于复杂的HTML的处理尤其是一些非法的有bug的html代码的处理那么最好还是用专门的HTML的解析的库。
Python中的专门用于HTML解析的库比较好用的就是BeautifulSoup。 BeautifulSoup简介
Python中专门用于HTML/XML解析的库
特点是
即使是有bug有问题的html代码也可以解析。
功能很强大 BeautifulSoup的主页是
http://www.crummy.com/software/BeautifulSoup/ BeautifulSoup的版本
BeautifulSoup主要有两个版本 BeautifulSoup 3
之前的比较早的是3.x的版本。
BeautifulSoup 3的在线文档
最新的可用的在线文档是
http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html
中文版的是
http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html 下载BeautifulSoup 3
http://www.crummy.com/software/BeautifulSoup/bs3/download//3.x/
中可以下载到很多版本比如我常用的3.0.6的版本
BeautifulSoup-3.0.6.py
http://www.crummy.com/software/BeautifulSoup/bs3/download//3.x/BeautifulSoup-3.0.6.py BeautifulSoup 4缩写为bs4
最新的v4版本的BeautifulSoup改名为bs4了。 注意
使用bs4时导入BeautifulSoup的写法是
from bs4 import BeautifulSoup;
然后就可以像之前3.x中一样直接使用BeautifulSoup了。
详见
【已解决】Python3中已经安装了bs4Beautifulsoup 4了但是却还是出错ImportError: No module named BeautifulSoup bs4的在线文档
http://www.crummy.com/software/BeautifulSoup/bs4/doc/ 下载bs4
http://www.crummy.com/software/BeautifulSoup/bs4/download/
可以下载到对应的bs4的版本比如
此时最新的版本是
beautifulsoup4-4.1.3.tar.gz
http://www.crummy.com/software/BeautifulSoup/bs4/download/beautifulsoup4-4.1.3.tar.gz
BeautifulSoup的用法
如何安装BeautifulSoup
3.0.6之前无需安装放到和Python文件同目录下即可使用
3.0.6之前都是不需要安装的所以使用起来最简单直接下载对应的版本比如
http://www.crummy.com/software/BeautifulSoup/bs3/download//3.x/BeautifulSoup-3.0.6.py
得到了BeautifulSoup-3.0.6.py然后改名为BeautifulSoup.py
然后放到和你当前的python文件同目录下比如我当前python文件是
D:\tmp\tmp_dev_root\python\beautifulsoup_demo\beautifulsoup_demo.py
那就放到
D:\tmp\tmp_dev_root\python\beautifulsoup_demo\
下面和beautifulsoup_demo.py同目录。 3.0.6之后需要安装BeautifulSoup后才可使用
关于如何安装一个Python的第三方模块简单说就是进入对应目录运行
setup.py install
详细解释可参考
【总结】Python安装第三方的库、package的方法 如何使用BeautifulSoup
在你的Python文件此处为beautifulsoup_demo.py中直接import即可。 关于示例html代码比如使用
【教程】抓取网并提取网页中所需要的信息 之 Python版 相关参考文档
3.x版本的
find(name, attrs, recursive, text, **kwargs) 使用BeautifulSoup提取html中的某个内容
关于最简单的最基本的用法提取html中的某个内容具体用法就死使用对应的find函数。
完整代码是 #!/usr/bin/python
# -*- coding: utf-8 -*-
Function:
【教程】Python中第三方的用于解析HTML的库BeautifulSoup
http://www.crifan.com/python_third_party_lib_html_parser_beautifulsoup
Author: Crifan Li
Version: 2012-12-26
Contact: admin at crifan dot com
from BeautifulSoup import BeautifulSoup;
def beautifulsoupDemo():
demoHtml
html
body
div classicon_col
h1 classh1usercrifan/h1
/div
/body
/html
;
soup BeautifulSoup(demoHtml);
print type(soup),type(soup); #type(soup) type instance
print soup,soup;
# 1. extract content
# method 1: no designate para name
#h1userSoup soup.find(h1, {class:h1user});
# method 2: use para name
h1userSoup soup.find(nameh1, attrs{class:h1user});
# more can found at:
#http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html#find%28name,%20attrs,%20recursive,%20text,%20**kwargs%29
print h1userSoup,h1userSoup; #h1userSoup h1 classh1usercrifan/h1
h1userUnicodeStr h1userSoup.string;
print h1userUnicodeStr,h1userUnicodeStr; #h1userUnicodeStr crifan
if __name__ __main__:
beautifulsoupDemo();
输出为
D:\tmp\tmp_dev_root\python\beautifulsoup_demobeautifulsoup_demo.py
type(soup) type instance
soup
html
body
div classicon_col
h1 classh1usercrifan/h1
/div
/body
/html
h1userSoup h1 classh1usercrifan/h1
h1userUnicodeStr crifan 使用BeautifulSoup修改/改变/替换原先html中的某个内容
如果需要改变原先html中的某个值可以参考官网解释
修改属性值
后来证实只能改Tag的中的属性的值不能改Tag的的值本身 完整示例代码为
#!/usr/bin/python
# -*- coding: utf-8 -*-
Function:
【教程】Python中第三方的用于解析HTML的库BeautifulSoup
http://www.crifan.com/python_third_party_lib_html_parser_beautifulsoup
Author: Crifan Li
Version: 2013-02-01
Contact: admin at crifan dot com
from BeautifulSoup import BeautifulSoup;
def beautifulsoupDemo():
demoHtml
html
body
div classicon_col
h1 classh1usercrifan/h1
/div
/body
/html
;
soup BeautifulSoup(demoHtml);
print type(soup),type(soup); #type(soup) type instance
print soup,soup;
print {0:^80}.format( 1. extract content );
# method 1: no designate para name
#h1userSoup soup.find(h1, {class:h1user});
# method 2: use para name
h1userSoup soup.find(nameh1, attrs{class:h1user});
# more can found at:
#http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html#find%28name,%20attrs,%20recursive,%20text,%20**kwargs%29
print h1userSoup,h1userSoup; #h1userSoup h1 classh1usercrifan/h1
h1userUnicodeStr h1userSoup.string;
print h1userUnicodeStr,h1userUnicodeStr; #h1userUnicodeStr crifan
print {0:^80}.format( 2. demo change tag value and property );
print {0:-^80}.format( 2.1 can NOT change tag value );
print old tag value,soup.body.div.h1.string; #old tag value crifan
changedToString uCrifanLi;
soup.body.div.h1.string changedToString;
print changed tag value,soup.body.div.h1.string; #changed tag value CrifanLi
print After changed tag value, new h1,soup.body.div.h1; #After changed tag value, new h1 h1 classh1usercrifan/h1
print {0:-^80}.format( 2.2 can change tag property );
soup.body.div.h1[class] newH1User;
print changed tag property value,soup.body.div.h1; #changed tag property value h1 classnewH1Usercrifan/h1
if __name__ __main__:
beautifulsoupDemo(); 总结
更多的用法和使用心得部分内容已整理到
【总结】Python的第三方库BeautifulSoup的使用心得
【整理】关于Python中的html处理库函数BeautifulSoup使用注意事项 有空再统一整理到
BeautifulSoup
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/89833.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!