-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_hanlder.py
38 lines (31 loc) · 1.12 KB
/
html_hanlder.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from HTMLParser import HTMLParser
import urlparse
import re
class HTMLHanlder(HTMLParser):
def __init__(self, base_url, page_url):
HTMLParser.__init__(self)
self.base_url = base_url
self.page_url = page_url
self.links = set()
self.datas = set()
# When we call HTMLParser feed() this function is called when it encounters an opening tag <a>
def handle_starttag(self, tag, attrs):
if tag == 'a':
for (attribute, value) in attrs:
if attribute == 'href':
url = value
mt = re.match(r'(http://www|www|https://www)\.[a-z0-9-]*\.([a-z]{2,3}|com\.cn)', value, re.I)
if not mt:
url = urlparse.urljoin(self.base_url, value)
self.links.add(url)
def handle_data(self, data):
mt = re.findall(ur"([\u4e00-\u9fa5]+).+", data)
for val in mt:
if len(val) > 1:
self.datas.add(val)
def page_links(self):
return self.links
def error(self, message):
pass