forked from AnthonySigogne/keyword-mining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.py
58 lines (51 loc) · 1.17 KB
/
url.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Toolbox for URLs.
"""
__author__ = "Anthony Sigogne"
__copyright__ = "Copyright 2017, Byprog"
__email__ = "anthony@byprog.com"
__license__ = "MIT"
__version__ = "1.0"
import re
import langdetect
import html2text
import requests
from HTMLParser import HTMLParser
def crawl(url) :
"""
Crawl an URL.
Return URL data.
"""
try :
r = requests.get(url)
except :
return None
return r
def extract_content(html) :
"""
Extract the main text content of a page.
"""
h = html2text.HTML2Text()
return h.handle(html)
def extract_title(html) :
"""
Extract the title of a page.
"""
h = HTMLParser()
try :
title = h.unescape(re.search("<title>([^<]+)</title>", html).group(1))
except :
title = "" # no title on page
return title
def extract_description(html) :
"""
Extract the description of a page.
"""
h = HTMLParser()
try :
description = h.unescape(re.search('<meta name="[^">]*description"[^">]*content="([^">]+)',html).group(1))
except :
description = "" # no description on page
return description