forked from katryo/bing_search_naive_bayes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_page.py
More file actions
24 lines (19 loc) · 724 Bytes
/
web_page.py
File metadata and controls
24 lines (19 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests
import cchardet
import re
class WebPage():
def __init__(self, url=''):
self.url = url
def fetch_html(self):
try:
response = requests.get(self.url)
self.set_html_body_with_cchardet(response)
except requests.exceptions.ConnectionError:
self.html_body = ''
def set_html_body_with_cchardet(self, response):
encoding_detected_by_cchardet = cchardet.detect(response.content)['encoding']
response.encoding = encoding_detected_by_cchardet
self.html_body = response.text
def remove_html_tags(self):
html_tag_pattern = re.compile('<.*?>')
self.html_body = html_tag_pattern.sub('', self.html_body)