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] ...
随机推荐
- Sql2005 全文索引详解
1.前言 14.1 全文索引的介绍 14.2 全文索引中常用的术语 14.3 全文索引的体系结构 14.4 全文目录管理 14.4.1 创建全文目录 14.4.2 查看与修改全文目录 14 ...
- Android onTouchEvent事件中onTouch方法返回值介绍
1.若return false说明没有成功执行onTouch事件,在执行完onTouch里面的代码之后,onTouch事件并没有结束.因此某些组件如Gallery会自动执行它所在view里onTouc ...
- C++基类和派生类之间的转换
本文讲解内容的前提是派生类继承基类的方式是公有继承,关键字public 以下程序为讲解用例. #include<iostream> using namespace std; class A ...
- linux 打开远程samba服务器
sudo mount -t cifs //10.104.*.*data /home/leeyoung/samba/ -o username=123,password=123
- leetcode@ [289] Game of Life (Array)
https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- acm-DP整理
一.背包 .各种01背包 void leastOne_Pack(int id, int num ) {//至少取一个: int i, j, c, v ; ; i <= num ; i ++ ) ...
- ffmpeg 的tutorial
可能是新的: https://github.com/chelyaev/ffmpeg-tutorial https://github.com/chelyaev/ffmpeg-tutorial.git 老 ...
- 独树一帜的字符串匹配算法——RK算法
参加了雅虎2015校招,笔试成绩还不错,谁知初面第一题就被问了个字符串匹配,要求不能使用KMP,但要和KMP一样优,当时瞬间就呵呵了.后经过面试官的一再提示,也还是没有成功在面试现场写得.现将该算法记 ...
- SOD-80 LL34 DL-35 (2.7~75V)贴片稳压二极管【worldsing 笔记
¨ Silicon Planar Zener Diodes ¨ In Mini-MELF case especially for automatic insertion. ¨ The Zener vo ...