在学习python的过程中,做练习,解析https://www.python.org/events/python-events/ HTML文件,输出Python官网发布的会议时间、名称和地点。

对html的解析是网页抓取的基础,分析抓取的结果找到自己想要的内容或标签以达到抓取的目的。

HTMLParser是python用来解析html的模块。它可以分析出html里面的标签、数据等等,是一种处理html的简便途径。 HTMLParser采用的是一种事件驱动的模式,当HTMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理它主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然后重新定义这几个以handler_开头的函数即可。

代码如下:

#coding:utf-8
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import os,string,urllib2 '''
HTMLParser的成员函数: handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx>
handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
handle_pi 处理形如<?instruction>的东西 '''
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.flag=None def handle_starttag(self, tag, attrs):
if tag == 'h3' and attrs.__contains__(('class', 'event-title')):
print '\n\n会议主题:',
self.flag=True #在需要打印的块中设置标识
elif tag == 'time':
print '\n会议时间:',
self.flag=True
elif tag == 'span' and attrs.__contains__(('class', 'event-location')):
print '\n会议地址:',
self.flag=True def handle_endtag(self, tag):
if tag in('h3','time','span'):
self.flag=None
#print('</%s>' % tag) def handle_startendtag(self, tag, attrs):
#print('<%s/>' % tag)
pass def handle_data(self, data):
if self.flag: #判断是需要的值才打印
print('%s' % data), #末尾加逗号打印不换行 def handle_comment(self, data):
#print('<!-- -->')
pass def handle_entityref(self, name):
if name == 'ndash':
print '至',
else:
pass def handle_charref(self, name):
#print('&#%s;' % name)
pass #f=open('python.html','r++')
#data=f.read()
#f.close()
pyhtml=urllib2.urlopen('https://www.python.org/events/python-events/').read()
parser = MyHTMLParser()
parser.feed(pyhtml)

执行结果:

[root@study lzc]# python h.py 

会议主题: PyCon US
会议时间: May 至 June
会议地址: Portland, Oregon, United States 会议主题: PyConTW in Taiwan
会议时间: June 至 June
会议地址: Academia Sinica, Academia Road, Section , Nankang, Taipei , Taiwan 会议主题: GeoPython
会议时间: June 至 June
会议地址: University of Applied Sciences and Arts Northwestern Switzerland, Basel, Switzerland 会议主题: PyCon Singapore
会议时间: June 至 June
会议地址: National University of Singapore, School of Computing, Computing , Computing Drive, Singapore , Republic of Singapore 会议主题: PyGotham
会议时间: July 至 July
会议地址: New York, NY, USA 会议主题: EuroPython
会议时间: July 至 July
会议地址: ECC, Bilbao, Basque Country, Spain 会议主题: PyData Berlin
会议时间: May 至 May
会议地址: Kosmos, Karl-Marx-Allee 131a, Berlin, Germany 会议主题: SciPy Latin America
会议时间: May 至 May
会议地址: Florianópolis - State of Santa Catarina, Brazil

python学习(解析python官网会议安排)的更多相关文章

  1. go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE

    go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE Go语言是谷歌2009发布的专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速 ...

  2. Python学习day09 - Python进阶(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. Python学习day05 - Python基础(3) 格式化输出和基本运算符

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. Python 3.6.3 官网 下载 安装 测试 入门教程 (windows)

    1. 官网下载 Python 3.6.3 访问 Python 官网 https://www.python.org/ 点击 Downloads => Python 3.6.3 下载 Python ...

  5. python 笔记1:官网下载及安装python;eclipse中安装配置pydev

    1  下载安装python. 官网:https://www.python.org/downloads/     根据自己的操作系统选择需要的版本下载并安装. 我的电脑操作系统windows xp的,只 ...

  6. Beam编程系列之Python SDK Quickstart(官网的推荐步骤)

    不多说,直接上干货! https://beam.apache.org/get-started/quickstart-py/ Beam编程系列之Java SDK Quickstart(官网的推荐步骤)

  7. 【Spark深入学习 -16】官网学习SparkSQL

    ----本节内容-------1.概览        1.1 Spark SQL        1.2 DatSets和DataFrame2.动手干活        2.1 契入点:SparkSess ...

  8. 【Python学习 】Python实现的FTP上传和下载功能

    一.背景 最近公司的一些自动化操作需要使用Python来实现FTP的上传和下载功能.因此参考网上的例子,撸了一段代码来实现了该功能,下面做个记录. 二.ftplib介绍 Python中默认安装的ftp ...

  9. python学习: 优秀Python学习资源收集汇总--转

    Python是一种面向对象.直译式计算机程序设计语言.它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用縮进来定义语句块.与Scheme.Ruby.Perl ...

随机推荐

  1. es6 ... 表达

    ... 包含0个或n个属性, { ...state.counters [id]: state.counters[id] - 1 } 只改变其中的id相对应的属性,其他保持不变

  2. python3-day3-python基础3

    一.字典 key:valuekey定义规则:1.必须是不可变的:数字,字符串,元祖,可hash2.key是唯一的 ,不可重复 value定义规则:任意类型增:dic["key"]= ...

  3. [转]Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only

    Okay for centos 6.4 also On apu.0xdata.loc, after this install was done $ which python /usr/local/bi ...

  4. Node.js文件系统、路径的操作详解

    17173 17173 2 75 2014-12-12T05:06:00Z 2014-12-12T05:06:00Z 21 2735 15595 www.17173.com 129 36 18294 ...

  5. SQL语句之备份表

    SELECT INTO 语句:表示从一个表中选取数据,然后把数据插入另一个表中,常用来备份一张表 1.全表结构备份: SELECT * INTO new_table_name FROM old_tab ...

  6. Android屏幕适配全攻略 (转载)

    http://blog.csdn.net/jdsjlzx/article/details/45891551 https://github.com/hongyangAndroid/AndroidAuto ...

  7. the import javax.servlet

    问题: 在eclipse中编写servlet时出现“the import javax.servlet unresolved” 问题原因: 缺少servlet-api.jar架包. 问题解决办法: 在e ...

  8. 查看最点CPU的语句

    select c.last_execution_time,c.execution_count,c.total_logical_reads, c.total_logical_writes,c.total ...

  9. c++容器

    1.vector:实质是动态堆数组,连续存储的内存区域,支持快速的随机访问. 2.list:实质是双向循环链表,支持在中间进行快速的插入删除,但是不能支持快速的随机访问.非连续的内存区域. 3.deq ...

  10. Struts2中使用OGNL

    链接:Struts标签库 OGNL(Object Graphic Navigatino Language) OGNL称为对象图导航语言.以一个对象为根(起点),通过OGNL可以访问与这个对象关联的其它 ...