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
- Python 3中HTTPparse 的使用
- python 中configparser的使用
- Python中类的使用
- python中关键字的使用
- Python中线程的使用
- python中线程的使用
- python中字符串的使用
- python中数组的使用
- python中线程的使用
- python中全局变量的使用
- python中match的使用
- python中PyQwt的使用
- python 中with的使用
- Python中ghost的使用
- python中eval的使用
- python中进程的使用
- python中线程的使用
- python中re的使用
- Vim中如何全选复制粘贴
- java成员内部类学习笔记
- Hdu 1087
- 【LintCode】最多有多少个点在一条直线上
- 转载的arcgis for flex api version3.7 教程,多学习啊!!!
- Python 3中HTTPparse 的使用
- HTML5定稿,为什么是原生App的颠覆
- 用 OProfile 彻底了解性能
- 互联网金融乱弹2:互联网金融Webservice接口开发实践
- C#读取XML流数据
- hdu杭电 2141 Can you find it? 【二分 N*logN】
- leetcode 070 —— Climbing Stairs
- 树和二叉树的学习——树的编号,nyoj,小猴子下落 就可以仿写
- Activity在onDestory()后,activity的实例还存在吗