【脚本语言系列】关于Python文本解析HTML,你需要知道的事
来源:互联网 发布:西门子plm软件是什么 编辑:程序博客网 时间:2024/04/29 23:06
如何使用HTMLParser
- 获取页面图片地址(www.baidu.com)
# -*- coding:utf-8 -*-#import Tkinterimport urllibimport HTMLParserclass MyHTMLParser(HTMLParser.HTMLParser): def __init__(self): HTMLParser.HTMLParser.__init__(self) self.gifs = [] self.jpgs = [] def handle_starttag(self, tags, attrs): if tags == "img": for attr in attrs: for t in attr: if "gif" in t: self.gifs.append(t) elif "jpg" in t: self.jpgs.append(t) elif "jpeg" in t: self.jpgs.append(t) else: pass def get_gifs(self): return self.gifs def get_jpgs(self): return self.jpgsclass Window: def __init__(self, root): self.root = root self.label = Tkinter.Label(root, text = "Input URL:") self.label.place(x = 5, y = 15) self.entryUrl = Tkinter.Entry(root, width = 30) self.entryUrl.place(x = 65, y = 15) self.get = Tkinter.Button(root, text = 'Get Picture', command = self.Get) self.get.place(x = 280, y = 15) self.edit = Tkinter.Text(root, width = 470, height = 600) self.edit.place(y = 50) def Get(self): url = self.entryUrl.get() page = urllib.urlopen(url) data = page.read() parser = MyHTMLParser() # print data parser.feed(data) self.edit.insert(Tkinter.END, "====GIF====\n") gifs = parser.get_gifs() for gif in gifs: self.edit.insert(Tkinter.END, gif + '\n') self.edit.insert(Tkinter.END, "===========\n") self.edit.insert(Tkinter.END, "====JPG====\n") jpgs = parser.get_jpgs() for jpg in jpgs: self.edit.insert(Tkinter.END, jpg + '\n') self.edit.insert(Tkinter.END, "===========\n") page.close()root = Tkinter.Tk()window = Window(root)root.minsize(600, 480)root.maxsize(600, 480)root.mainloop()
1. 用户界面
2. 界面输出
什么是HTMLParser
在Python中可以使用HTMLParser模块处理HTML, 获取页面中感兴趣的内容。
HTMLParser模块提供了对HTML标记处理的方法。
0 0
- 【脚本语言系列】关于Python文本解析HTML,你需要知道的事
- 【脚本语言系列】关于Python文本解析XML,你需要知道的事
- 【脚本语言系列】关于Python操作数据文本字符串,你需要知道的事
- 【脚本语言系列】关于Python系统管理,你需要知道的事
- 【脚本语言系列】关于Python容器,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理HTML,你需要知道的事
- 【脚本语言系列】关于Python调试报错,你需要知道的事
- 【脚本语言系列】关于Python开发环境搭建,你需要知道的事
- 【脚本语言系列】关于Python数值计算NumPy, 你需要知道的事
- 【脚本语言系列】关于Python测试工具lettuce, 你需要知道的事
- 【脚本语言系列】关于Python测试工具mock, 你需要知道的事
- 【脚本语言系列】关于Python测试工具unittest, 你需要知道的事
- 【脚本语言系列】关于Python测试工具docstrings, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pyflakes, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pylint, 你需要知道的事
- 【脚本语言系列】关于Python性能分析工具timeit, 你需要知道的事
- 【脚本语言系列】关于Python代码分析工具line_profiler,你需要知道的事
- 【脚本语言系列】关于Python游戏工具Pygame,你需要知道的事
- 软件测试江湖(三)心得技巧篇
- 【JavaScript】12种应该避免的js语法
- Java枚举
- Hadoop集群_MapReduce初级案例
- matrix 和 array的区别
- 【脚本语言系列】关于Python文本解析HTML,你需要知道的事
- MTk i2c读写例子
- Linux多文件编译技巧、同时打开多个文件、make等操作
- 260. Single Number III
- linux 查看磁盘空间大小
- nyoj-14 会场安排问题
- 厉害了,1GB内存的电脑也能流畅运行最新的PS!
- ios xcode 支持https 遇到的问题
- eclipse+mysql(navicat)通过jdbc连接