python中HTMLParser简单理解
找一个网页,例如https://www.python.org/events/python-events/,用浏览器查看源码并复制,然后尝试解析一下HTML,输出Python官网发布的会议时间、名称和地点。
- from html.parser import HTMLParser
- from html.entities import name2codepoint
- class MyHTMLParser(HTMLParser):
- in_title = False
- 7 in_loca = False
- in_time = False
- def handle_starttag(self,tag,attrs):
- if ('class','event-title') in attrs:
- self.in_title = True
- elif ('class','event-location') in attrs:
- self.in_loca = True
- elif tag == 'time':
- self.in_time = True
- self.times = []
- def handle_data(self,data):
- if self.in_title:
- print('-'*50)
- print('Title:'+data.strip())
- if self.in_loca:
- print('Location:'+data.strip())
- if self.in_time:
- self.times.append(data)
- def handle_endtag(self,tag):
- if tag == 'h3':self.in_title = False
- if tag == 'span':self.in_loca = False
- if tag == 'time':
- self.in_time = False
- print('Time:'+'-'.join(self.times))
- parser = MyHTMLParser()
- with open('s.html') as html:
- parser.feed(html.read())
重点理解15-17和30-32行,python的HTMLParser在解析网页中的文本时,是按照一个个字符串解析的,
<h3 class="event-title"><a href="/events/python-events/401/">PyOhio 2016</a></h3>
<span class="event-location">The Ohio Union at The Ohio State University. 1739 N. High Street, Columbus, OH 43210, USA</span>
<time datetime="2016-07-29T00:00:00+00:00">29 July – 01 Aug. <span class="say-no-more"> 2016</span></time>
在遇到特殊字符串时(例如–)会直接跳过,将前后作为两个字符串,15-17和30-32的配合是为了获取span中的年份2016
python中HTMLParser简单理解的更多相关文章
- python中一个简单的webserver
python中一个简单的webserver 2013-02-24 15:37:49 分类: Python/Ruby 支持多线程的webserver 1 2 3 4 5 6 7 8 9 10 11 ...
- Python 中lambda 简单介绍
转自:https://www.cnblogs.com/AlwaysWIN/p/6202320.html 在学习python的过程中,lambda的语法经常出现,现在将它整理一下,以备日后查看. 1.l ...
- python中Metaclass的理解
今天在学习<python3爬虫开发实战>中看到这样一段代码3 class ProxyMetaclass(type): def __new__(cls, name, bases, attrs ...
- Python中使用@的理解
Python函数中使用@ 稍提一下的基础 fun 和fun()的区别 以一段代码为例: def fun(): print('fun') return None a = fun() #fun函数并将返回 ...
- 转载-对于Python中@property的理解和使用
原文链接:https://blog.csdn.net/u013205877/article/details/77804137 重看狗书,看到对User表定义的时候有下面两行 @property def ...
- 深入理解python(一)python语法总结:基础知识和对python中对象的理解
用python也用了两年了,趁这次疫情想好好整理下. 大概想法是先对python一些知识点进行总结,之后就是根据python内核源码来对python的实现方式进行学习,不会阅读整个源码,,,但是应该会 ...
- Python中生成器的理解
1.生成器的定义 在Python中一边循环一边计算的机制,称为生成器 2.为什么要有生成器 列表所有的数据都存在内存中,如果有海量的数据将非常耗内存 如:仅仅需要访问前面几个元素,那后面绝大多数元素占 ...
- Python中yield深入理解
众所周知,python中的yield有这样的用法: def test(alist): for i in alist: yield i 这样,这个test函数就变成了一个生成器,当每次调用的时候,就会自 ...
- python中切片的理解
Python中什么可以切片 l Python中符合序列的有序序列都支持切片(slice) l 如:列表,字符,元祖 Python中切片的格式 l 格式:[start : end : step] ...
随机推荐
- Struts2 教程
一.Struts2是什么 Struts2是在WebWork2基础发展而来的.和Struts1一样, Struts2也是基于MVC的web层框架. 那么既然有了Struts1,为何还要Struts2? ...
- Javascript 对输入框中的内容进行 “全选/反选”
<</span>script> document.write("<</span>ul>"); for(var i=0;i<&l ...
- HDU5692 Snacks DFS+线段树
分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时 只会影响的子树的和,最优值也是子树最大的值 #include <cstdio> #include < ...
- python 中 struct 用法
下面就介绍这个模块中的几个方法. struct.pack():我的理解是,python利用 struct模块将字符(比如说 int,long ,unsized int 等)拆成 字节流(用十六进制表示 ...
- Hibernate学习笔记(四)关系映射之一对一关联映射
一. 一对一关联映射 ² 两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ² 有两种策略可以实现一对一的关联映射 Ø 主键关联:即让 ...
- Bzoj-2005 能量采集 gcd,递推
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 题意:题目转换后的模型就是求Σ(gcd(x,y)), 1<=x<=n, ...
- Spark生态
- EventBus使用小记
自从使用了EventBus,代码干净了好多. 从此你不用startActivityForResult了,从此你不用再写注册BroadcastReceiver了,从此你不用再写一些回调了. 只需要在需要 ...
- 转载.NET 4.0中的泛型的协变和逆变
先做点准备工作,定义两个类:Animal类和其子类Dog类,一个泛型接口IMyInterface<T>, 他们的定义如下: public class Animal { } public ...
- HDU 1226 BFS
注意密码位数<=500 输出注意十六进制改成字母 要点题目都已说明 ac就好 #include<iostream> #include<stdio.h> #include& ...