Python:[转]浅学BeautifulSoup分析html
来源:互联网 发布:淘宝c店保证金是多少 编辑:程序博客网 时间:2024/06/05 03:37
http://www.cnblogs.com/yupeng/p/3362031.html
部分内容:
soup 就是BeautifulSoup处理格式化后的字符串,soup.title 得到的是title标签,soup.p 得到的是文档中的第一个p标签,要想得到所有标签,得用find_all函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西。
get_text() 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的。你可以试试 print soup.p.get_text(),其实是可以获得标签的其他属性的,比如我要获得a标签的href属性的值,可以使用 print soup.a['href'],类似的其他属性,比如class也是可以这么得到的(soup.a['class'])。
特别的,一些特殊的标签,比如head标签,是可以通过soup.head 得到
http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html
部分内容:
0、初始化:
1 soup = BeautifulSoup(html) # html为html源代码字符串,type(html) == str
1、用tag获取相应代码块的剖析树:
既然要分析html,首先要找到对我们有用的tag块,beautiful提供了非常便利的方式。
#当用tag作为搜索条件时,我们获取的包含这个tag块的剖析树:#<tag><xxx>ooo</xxx></tag>#这里获取head这个块head = soup.find('head')# or# head = soup.head# or# head = soup.contents[0].contents[0]
运行后,我们会得到:
1 <head>2 <title>Page title</title>3 </head>
这里小灯我还是推荐使用第二种方法,find方法在当前tag剖析树(当前这个html代码块中)寻找符合条件的子树并返回。find方法提供多种查询方式,包括用喜闻乐见的regex哦~之后会详细介绍。
contents属性是一个列表,里面保存了该剖析树的直接儿子。
如:
1 html = soup.contents[0] # <html> ... </html>2 head = html.contents[0] # <head> ... </head>3 body = html.contents[1] # <body> ... </body>
2、用contents[], parent, nextSibling, previousSibling寻找父子兄弟tag
为了更加方便灵活的分析html代码块,beautifulSoup提供了几个简单的方法直接获取当前tag块的父子兄弟。
假设我们已经获得了body这个tag块,我们想要寻找<html>, <head>, 第一个<p>, 第二个<p>这四个tag块:
# body = soup.bodyhtml = body.parent # html是body的父亲head = body.previousSibling # head和body在同一层,是body的前一个兄弟p1 = body.contents[0] # p1, p2都是body的儿子,我们用contents[0]取得p1p2 = p1.nextSibling # p2与p1在同一层,是p1的后一个兄弟, 当然body.content[1]也可得到print p1.text# u'This is paragraphone.'print p2.text# u'This is paragraphtwo.'# 注意:1,每个tag的text包括了它以及它子孙的text。2,所有text已经被自动转#为unicode,如果需要,可以自行转码encode(xxx)
然而,如果我们要寻找祖先或者孙子tag怎么办呢?? 用while循环吗? 不, beautifulsoup已经提供了方法。
3、用find, findParent, findNextSibling, findPreviousSibling寻找祖先或者子孙 tag:
有了上面的基础,这里应该很好理解了,例如find方法(我理解和findChild是一样的),就是以当前节点为起始,遍历整个子树,找到后返回。
而这些方法的复数形式,会找到所有符合要求的tag,以list的方式放回。他们的对应关系是:find->findall, findParent->findParents, findNextSibling->findNextSiblings...
如:
1 print soup.findAll('p')2 # [<p id="firstpara" align="center">This is paragraph <b>one</b>.</p>, <p id="secondpara" align="blah">This is paragraph <b>two</b>.</p>]
这里我们重点讲一下find的几种用法,其他的类比:
find(name=None, attrs={}, recursive=True, text=None, **kwargs)
(ps:只讲几种用法,完整请看官方link : http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html#The%20basic%20find%20method:%20findAll%28name,%20attrs,%20recursive,%20text,%20limit,%20**kwargs%29)
1) 搜索tag:
1 find(tagname) # 直接搜索名为tagname的tag 如:find('head')2 find(list) # 搜索在list中的tag,如: find(['head', 'body'])3 find(dict) # 搜索在dict中的tag,如:find({'head':True, 'body':True})4 find(re.compile('')) # 搜索符合正则的tag, 如:find(re.compile('^p')) 搜索以p开头的tag5 find(lambda) # 搜索函数返回结果为true的tag, 如:find(lambda name: if len(name) == 1) 搜索长度为1的tag6 find(True) # 搜索所有tag
2) 搜索属性(attrs):
1 find(id='xxx') # 寻找id属性为xxx的2 find(attrs={id=re.compile('xxx'), algin='xxx'}) # 寻找id属性符合正则且algin属性为xxx的3 find(attrs={id=True, algin=None}) # 寻找有id属性但是没有algin属性的
3) 搜索文字(text):
注意,文字的搜索会导致其他搜索给的值如:tag, attrs都失效。
方法与搜索tag一致
4) recursive, limit:
recursive=False表示只搜索直接儿子,否则搜索整个子树,默认为True。
当使用findAll或者类似返回list的方法时,limit属性用于限制返回的数量,如findAll('p', limit=2): 返回首先找到的两个tag
*4、用next,previous寻找上下文tag(少用)
这里我们主要看看next, next是取得当前的tag的下一个(按代码从上到下的顺序)tag块。这与contents是不一样的,千万别混淆了哦^ ^
我们举个栗子来看看
1 <a>2 a3 <b>b</b>4 <c>c</c>5 </a>
我们看看next的实际效果:
1 a = soup.a2 b = soup.b3 n1 = b.next4 n2 = n1.next
输出一下:
1 print a.next2 # u'a'3 print n14 # u'b'5 print n26 # <c>c</c>
所以,next仅仅是获取文档上的“下一个”的tag,和剖析树中的位置无关。
当然也有findNext和findAllNext方法。
至于previous, 表示上一个tag块,就类比一下吧~^ ^
- Python:[转]浅学BeautifulSoup分析html
- 用python的BeautifulSoup分析html
- 用python的BeautifulSoup分析html
- [学习]用python的BeautifulSoup分析html
- 用python的BeautifulSoup分析html
- 用python的BeautifulSoup分析html
- 学习]用python的BeautifulSoup分析html
- 用BeautifulSoup分析html
- 【Python】 html解析BeautifulSoup
- [Python下载CSDN博客]2. 使用BeautifulSoup分析HTML(一)
- [Python下载CSDN博客]2. 使用BeautifulSoup分析HTML(二)
- Python获取网页内容、使用BeautifulSoup库分析html
- python解析html之BeautifulSoup
- Python 用BeautifulSoup 解析Html
- 浅学python
- 用Python解析HTML,BeautifulSoup使用简介
- python : BeautifulSoup 网页HTML 解析器
- [Python]BeautifulSoup—HTML解析包
- 生活已不易!且行且珍惜!
- stanford open-source CFD code SU2-3.2.9 源码编译安装
- Robot Framework Get Library Instance
- Java版 求杨辉三角
- 第八周 项目三(2)分数类中的运算符重载
- Python:[转]浅学BeautifulSoup分析html
- poj 2084
- Leetcode 3Sum Closest
- Qt Browser
- 计数排序
- 栈和队列(5.18)
- 多线程:常见题目
- 开机时间太长
- 二分查找