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] ...
随机推荐
- 每天一个linux 命令:which
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. ...
- 在ASP.NET MVC中修改默认代码生成/支架模板
到 C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual ...
- tomcat 7 中的类加载器学习
tomcat 7自带很多junit测试用例,可以帮助我们窥探源码的秘密.以下使用来测试类加载器的一个测试用例.类加载器也是对象,他们用来将类从类从.class文件加载到虚拟机,这些已经讲了很多,深入j ...
- bzoj 1040 [ZJOI2008]骑士(基环外向树,树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1040 [题意] 给一个基环森林,每个点有一个权值,求一个点集使得点集中的点无边相连且权 ...
- uva 11991 Easy Problem from Rujia Liu? vector+map
水题 学习一下数据的存储方法. #include<iostream> #include<cstdio> #include<cstdlib> #include< ...
- Magento 多语言
一: 1>进入后台选择如下: 2> 显示页面如下: 输入后台登陆的用户名和密码. 3>然后去Magento官网搜索一下 Magento Official Chinese Transl ...
- 在C中定义一个动态的二维数组
一般来讲两种办法: 第一种:连续内存分配 #include "stdio.h" #include "stdlib.h" int main() { int x,y ...
- 【转载】locate命令的使用
[说明]转载自 http://www.cnblogs.com/flysnail/archive/2012/05/16/2504266.html 使用locate命令,遇到了这样的情况:当前目录下有一个 ...
- eclipse下使用tomcat启动maven项目
最近学习使用maven,建立了一个maven项目使用eclipse下tomcat启动时报错: 严重: ContainerBase.addChild: start: org.apache.catalin ...
- Use ALTER DATABASE to Move Databases
Use ALTER DATABASE to Move Databases Follow Our Daily Tips •facebook.com/TechNetTips• twitter.com/Te ...