Python 分析html

Python 分析html

 

分析我们可以有4钟方式:

  1. 字符串分析
  2. 使用正则表达式 
  3. 使用 python提供内置库 html parser
  4. 使用第三方库  BeautifulSoup

 

第一种就是你自己把真个html内容看成一个字符串,然后查找,截取等操作完成。关于使用正则表达式我们前面有一篇文章介绍:

Python 正则表达式基础

内置库 htmlparser,请参看官方文档,

HTMLParser

使用方法很简单,下面给出官方的一段示例代码:

from HTMLParser import HTMLParser

# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print "Encountered a start tag:", tag
    def handle_endtag(self, tag):
        print "Encountered an end tag :", tag
    def handle_data(self, data):
        print "Encountered some data  :", data

# instantiate the parser and fed it some HTML
parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head>'
            '<body><h1>Parse me!</h1></body></html>')

输出结果如下:

Encountered a start tag: html
Encountered a start tag: head
Encountered a start tag: title
Encountered some data  : Test
Encountered an end tag : title
Encountered an end tag : head
Encountered a start tag: body
Encountered a start tag: h1
Encountered some data  : Parse me!
Encountered an end tag : h1
Encountered an end tag : body
Encountered an end tag : html

 

但是HtmlParser功能还是有限,第四种方式就是本文推荐的方式,因为使用更加方便(跟htmlparser比较),功能更加强大。虽然是第三方库,但是安装还是很方便的。

名字:BeautifulSoup

官网:http://www.crummy.com/software/BeautifulSoup

安装:

如果你是debian系列的系统,如Ubuntu,那么

$ apt-get install python-bs4

如果你安装了python得 easy_install或者 pip,那么安装更加方便

$ easy_install beautifulsoup4

$ pip install beautifulsoup4

如果这些都没有装,没关系,从官网下载最新的包,解压,然后进入解压,执行下面的命令,这种方式特别适合Windows的环境。

$ python setup.py install

使用:

只要导入相应的库 BeautifulSoup就可以使用了

from bs4 import BeautifulSoup as BSoup

soup = BSoup( htmlContent )

下面给出一段示例代码:

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
soup.title
# <title>The Dormouse's story</title>

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# <p class="title"><b>The Dormouse's story</b></p>

soup.p['class']
# u'title'

soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

 

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

Leave a Reply

Your email address will not be published. Required fields are marked *