]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_beautifulsoup4.py
HTML parser based on BeautifulSoup4
[bookmarks_db.git] / parse_html / bkmk_ph_beautifulsoup4.py
1 """HTML Parser using BeautifulSoup4
2
3 This file is a part of Bookmarks database and Internet robot.
4
5 """
6
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2017 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['parse_html']
12
13
14 import re
15 from bs4 import BeautifulSoup
16 from .bkmk_ph_util import HTMLParser
17
18 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
19
20 def _parse_html(html_text, charset):
21     try:
22         return BeautifulSoup(html_text, from_encoding=charset)
23     except TypeError:
24         return None
25
26 def parse_html(html_text, charset=None, log=None):
27     root = _parse_html(html_text, charset)
28     if root is None:
29         return None
30
31     _charset = root.originalEncoding
32     html = root.html
33     if html is None:
34         html = root
35
36     head = html.head
37     if head is None:
38         head = html # Some sites put TITLE in HTML without HEAD
39
40     title = head.title
41     if (title is None) and (html is not head):
42         # Some sites put TITLE in HTML outside of HEAD
43         title = html.title
44
45     if title is None:
46         # Lookup TITLE in the root
47         title = root.title
48
49     if title is not None:
50         if title.string:
51             title = title.string
52         else:
53             parts = []
54             for part in title:
55                 if not isinstance(part, basestring):
56                     part = unicode(part)
57                 parts.append(part.strip())
58             title = ''.join(parts)
59
60     meta = head.find(_find_contenttype, recursive=False)
61     if meta:
62         try:
63             meta_content = meta.get("content")
64             if meta_content:
65                 __charset = meta_content.lower().split('charset=')[1].split(';')[0]
66             else:
67                 __charset = False
68         except IndexError: # No charset in the META Content-Type
69             meta_charset = False
70         else:
71             meta_charset = _charset = __charset
72     else:
73         meta_charset = False
74
75     if not meta_charset:
76         meta = head.find(_find_charset, recursive=False)
77         if meta:
78             meta_content = meta.get("charset")
79             if meta_content:
80                 meta_charset = _charset = meta_content.lower()
81
82     if title and (_charset or meta_charset):
83         title = title.encode(_charset or meta_charset)
84
85     meta = head.find(_find_refresh, recursive=False)
86     if meta:
87         refresh = meta.get("content")
88     else:
89         refresh = None
90
91     meta = head.find(_find_icon, recursive=False)
92     if meta:
93         icon = meta.get("href")
94     else:
95         icon = None
96
97     if (title is None) and (refresh is None) and (icon is None):
98         return None
99     return HTMLParser(_charset, meta_charset, title, refresh, icon)
100
101 def _find_contenttype(Tag):
102     return (Tag.name == "meta") and \
103        (Tag.get_attribute_list("http-equiv", '')[0].lower() == "content-type")
104
105 def _find_charset(Tag):
106     return (Tag.name == "meta") and Tag.get("charset", '')
107
108 def _find_refresh(Tag):
109     return (Tag.name == "meta") and \
110        (Tag.get_attribute_list("http-equiv", '')[0].lower() == "refresh")
111
112 def _find_icon(Tag):
113     return (Tag.name == "link") and \
114        (Tag.get_attribute_list("rel", '')[0].lower() in ('icon', 'shortcut icon'))