-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathhtml_reader.py
65 lines (58 loc) · 2.3 KB
/
html_reader.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
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
#
# Copyright (c) 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import validators
import urllib.request
import re
from html.parser import HTMLParser
import logging as log
class HTMLDataExtractor(HTMLParser):
def __init__(self, tags):
super(HTMLDataExtractor, self).__init__()
self.started_tags = {k:[] for k in tags}
self.ended_tags = {k:[] for k in tags}
def handle_starttag(self, tag, attrs):
if tag in self.started_tags:
self.started_tags[tag].append([])
def handle_endtag(self, tag):
if tag in self.ended_tags:
txt = ''.join(self.started_tags[tag].pop())
self.ended_tags[tag].append(txt)
def handle_data(self, data):
for tag, l in self.started_tags.items():
for d in l:
d.append(data)
# read html urls and list of all paragraphs data
def get_paragraphs(url_list):
paragraphs_all = []
for url in url_list:
log.info("Get paragraphs from {}".format(url))
if not validators.url(url):
print("{} - is not valid")
continue
with urllib.request.urlopen(url) as response: # nosec
parser = HTMLDataExtractor(['title', 'p'])
charset='utf-8'
if 'Content-type' in response.headers:
m = re.match('.*charset=(\S+).*', response.headers['Content-type'])
if m:
charset = m.group(1)
data = response.read()
parser.feed(data.decode(charset))
title = ' '.join(parser.ended_tags['title'])
paragraphs = parser.ended_tags['p']
log.info("Page '{}' has {} chars in {} paragraphs".format(title, sum(len(p) for p in paragraphs), len(paragraphs)))
paragraphs_all.extend(paragraphs)
return paragraphs_all