python解析html tag
来源:互联网 发布:云协作软件 编辑:程序博客网 时间:2024/05/16 19:46
有时候网页上信息太多,一方面用肉眼看容易出错,另一方面点击了网页的链接之后,原来页面的信息就被刷新了,这时候如果能通过程序自动的分析网页上的信息就好了,python的HTMLParser能够很好的解决这个问题,当然它只是把内容抓取下来,具体分析还得看不同人的需求。
from HTMLParser import HTMLParserclass MyHTMLParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.links = [] def handle_starttag(self, tag, attrs): #print "hello" if tag == "a": if len(attrs) == 0: pass else: for (variable, value) in attrs: if variable == "href": self.links.append(value)if __name__ == "__main__": html_code = """ <a href="www.google.com">google.com</a> <A Href="www.sina.com.cn">Sina</a> """ hp = MyHTMLParser() hp.feed(html_code) hp.close() print(hp.links)
首先自定义一个类MyHTMLParser,从HTMLParser继承,重载handle_starttag()方法, 然后通过feed方法把html内容喂给MyHTMLParser对象,最后关闭就OK了。
在eshell中
$python htmlparser.py['www.google.com', 'www.sina.com.cn']
0 0
- python解析html tag
- HTML标签解析类 Tag Parse
- python题目-----匹配HTML Tag<.*>和<.*?>区别
- python过滤html文档中的Tag标签
- HTML Tag
- HTML tag
- html tag
- HTML TAG
- html tag
- HTML tag
- 用python解析html
- 用python解析html
- 用python解析html
- python解析html/xml
- python HTML解析器
- python html解析
- python 解析html
- python 解析HTML
- android 中怎么获取对象的值
- Object-C之基本数据类型
- Android之IntentService
- 微信小程序 - 开发工具快捷键
- Oracle DML触发器
- python解析html tag
- AngularJS初探
- jmeter 参数化的五种方法
- poj1330Nearest Common Ancestors(暴力与倍增)
- 【python】numpy数组(array)扩充(复制)方法repeat和tile的使用
- 是时候学习ReJava了
- Parameter server anatomy (1)
- HttpClient4.x使用中出现的几个问题及解决方法
- 文章标题