Python解析html

Python 的Beautiful Soup包可以方便的解析html

序言

  之前用python爬取网页的时候,一直用的是regex或者自带的库sgmllib里的SGMLParser。但是遇到复杂一点的情况时,SGMLParser往往就不那么给力了!(哈,难道说我 too native了?毕竟beautifulSoup是继承sgmlparser的么~)所以,我寻寻觅觅寻寻觅觅,发现了BeautifulSoup这么个玩意。BeautifulSoup提供了很人性化的parser tree,有了它,我们可以简单的抽取出tagname, attrs, text等等等等...

  install什么的,看这里 -> http://www.crummy.com/software/BeautifulSoup/

入门

(ps:其实入门什么的看官方文档是最好的了,这里只是记录一下简单的用法。)

(official document : http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html

  首先先介绍实际工作中最常用的几个方法:

  举例的html代码(就用官方例子好了):

<html>
    <head>
        <title>Page title</title>
    </head>
    <body>
        <p id="firstpara" align="center">
        This is paragraph<b>one</b>.
        </p>
        <p id="secondpara" align="blah">
        This is paragraph<b>two</b>.
        </p>
     </body>
</html>

0、初始化:

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]

    运行后,我们会得到:

<head>
<title>Page title</title>
</head>

  这里我还是推荐使用第二种方法,find方法在当前tag剖析树(当前这个html代码块中)寻找符合条件的子树并返回。find方法提供多种查询方式,包括用喜闻乐见的regex哦~之后会详细介绍。

   contents属性是一个列表,里面保存了该剖析树的直接儿子。

    如:

html = soup.contents[0] # <html> ... </html>
head = html.contents[0] # <head> ... </head>
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]取得p1
p2 = 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...

  如:

print soup.findAll('p')
# [<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:

find(tagname)        # 直接搜索名为tagname的tag 如:find('head')
find(list)           # 搜索在list中的tag,如: find(['head', 'body'])
find(dict)           # 搜索在dict中的tag,如:find({'head':True, 'body':True})
find(re.compile('')) # 搜索符合正则的tag, 如:find(re.compile('^p')) 搜索以p开头的tag
find(lambda)         # 搜索函数返回结果为true的tag, 如:find(lambda name: if len(name) == 1) 搜索长度为1的tag
find(True)           # 搜索所有tag

  2) 搜索属性(attrs):

find(id='xxx')                                  # 寻找id属性为xxx的
find(attrs={id=re.compile('xxx'), algin='xxx'}) # 寻找id属性符合正则且algin属性为xxx的
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是不一样的,千万别混淆了哦^ ^

  我们举个栗子来看看

<a>
    a
    <b>b</b>
    <c>c</c>
 </a>

    我们看看next的实际效果:

a = soup.a
b = soup.b
n1 = b.next
n2 = n1.next

   输出一下:

print a.next
# u'a'
print n1
# u'b'
print n2
# <c>c</c>

    所以,next仅仅是获取文档上的“下一个”的tag,和剖析树中的位置无关。

    当然也有findNext和findAllNext方法。

   至于previous, 表示上一个tag块,就类比一下吧~^ ^

   原博:http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html

更多

    Soupy也可以方便的解析html文档,它是基于Beautiful Soup的python库,与其相比,能够更快捷的写出复杂的html查询,

    github:

https://github.com/ChrisBeaumont/soupy?utm_content=buffer6e5b7&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer

    api:

http://soupy.readthedocs.org/en/stable/getting_started.html


    熟悉jquery也可用pyquery对html文档进行解析。


转载于:https://my.oschina.net/open4source/blog/401724

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值