-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_HTMLParser.py
More file actions
94 lines (71 loc) · 2.33 KB
/
Copy pathuse_HTMLParser.py
File metadata and controls
94 lines (71 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/env python3
# -*- coding:utf-8 -*-
# HTMLParser 解析HTML
from html.parser import HTMLParser
from html.entities import name2codepoint
class MyHTMLParser(HTMLParser): # 重载HTML解析器
def handle_starttag(self, tag, attrs):
print('<%s>' % tag)
def handle_endtag(self, tag):
print('</%s>' % tag)
def handle_startendtag(self, tag, attrs):
print('<%s/>' % tag)
def handle_data(self, data):
print(data)
def handle_comment(self, data):
print('<!--', data, '-->')
def handle_entityref(self, name):
print('&%s;' % name)
def handle_charref(self, name):
print('&#%s;' % name)
parser = MyHTMLParser()
# 调用feed方法,可多次调用
parser.feed('''<html>
<head></head>
<body>
<!-- test html parser -->
<p>Some <a href=\"#\">html</a> HTML tutorial...<br>END</p>
</body></html>''')
# example
from html.parser import HTMLParser
from urllib import request
import re
class MyHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.flag = False
self.result = []
# 临时变量
self.dict = {}
self.isHandling = ''
def handle_starttag(self, tag, attrs):
# 找到即将开始的会议
if tag == 'ul' and attrs[0][1] == 'list-recent-events menu':
self.flag = True
if self.flag == True:
# 分别处理title,time,location三个标签
# print('starttag: %s' % tag)
if tag == 'a':
self.isHandling = 'title'
if tag == 'time':
self.isHandling = 'time'
if tag == 'span':
self.isHandling = 'location'
def handle_endtag(self, tag):
if tag == 'ul' and self.flag == True:
self.flag = False
if self.flag == True and tag == 'li':
self.result.append(self.dict)
self.dict = {}
def handle_data(self, data):
if self.isHandling != '':
self.dict[self.isHandling] = data
self.isHandling = ''
parser = MyHTMLParser()
with request.urlopen('https://www.python.org/events/python-events/') as f:
data = f.read().decode('utf-8')
parser.feed(data)
for item in parser.result:
for (k, v) in item.items():
print('%s: %s' % (k, v))
print('-------------------')