Python 3中HTTPparse 的使用

来源:互联网 发布:法师升级数据 编辑:程序博客网 时间:2024/06/05 07:04

在python中能够进行html和xhtml的库有很多,如HTMLParser、sgmllib、htmllib、BeautifulSoup、mxTidy、uTidylib等,这里介绍一下HTMLParser、BeautifulSoup等模块。测试用的html 文件
<head> <title> XHTML 与" HTML 4.01 "标准没有太多的不同</title> </head> <body> i love÷ you× <a href="http://pypi.python.org/pypi" title="link1">我想你</a> <div id="m"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129" ></div> </body> </html>

以下是Python 3中的处理方法

from html.parser import HTMLParser from html.entities import name2codepointimport urllib.requestdef getimage(addr):    u = urllib.request.urlopen(addr)    data = u.read()    filename = addr.split('/')[-1]    f = open(filename, 'wb')    f.write(data)    f.close()    print(filename + '已经生成')    class MyHtmlParser(HTMLParser):    def __init__(self):        self.taglevels = []        self.handletags = ['title','body']        self.processing = None        HTMLParser.__init__(self)    def handle_starttag(self, tag, attrs):        if tag in self.handletags:            self.data = ''            self.processing = tag        if 'a' == tag:            for name, value in attrs:                if name == 'href':                    print('连接地址:'+ value)        if 'img' == tag:            for name,value in attrs:                if name == 'src':                    getimage(value)                        def handle_endtag(self, tag):        if tag in self.handletags:            print(str(tag) + ':' + str(tp.gettitle()))            self.processing = None    def handle_data(self, data):        if self.processing:            self.data +=data    def handle_entityref(self,name):         c = chr(name2codepoint[name])         self.handle_data(c)                    def gettitle(self):        return self.datafd = open('test1.html')tp = MyHtmlParser()tp.feed(fd.read())</span>        




0 0
原创粉丝点击