-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_tools.py
More file actions
57 lines (53 loc) · 1.48 KB
/
html_tools.py
File metadata and controls
57 lines (53 loc) · 1.48 KB
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
#----------------------------------------------------#
# *Python browser
# html_tools.py
#----------------
# Removes unwanted html quirks
#----------------------------------------------------#
ent_to_char = {
"nbsp": " ",
"lt": "<",
"gt": ">",
"amp": "&",
"quot": "\"",
"apos": "'",
"cent": "¢",
"pound": "£",
"yen": "¥",
"euro": "€",
"copy": "©",
"reg": "®"
}
#---------------------------------------------#
# *removeSpaces( html source )
# Removes trash from html source
def removeSpaces( html ):
global ent_to_char
# Remove spaces
html = html.replace( "\n", "" )
while html.find( " " ) != -1:
html = html.replace( " ", " " )
return html
#---------------------------------------------#
# *replaceHTMLEnts( html source )
# Replaces HTML entities to ready-to-process-code
def replaceHTMLEnts( html ):
pos = 0
while pos < len( html ):
pos = html.find( "&", pos )
if pos == -1: return html
semicolon_pos = html.find( ";", pos )
data = html[ pos:(semicolon_pos) ]
if semicolon_pos == -1:
pos += 1
else:
new_data = ""
#Handle data
if data[0] == "#":
new_data = chr( data[1:] )
elif data in ent_to_char:
new_data = ent_to_char[ data ]
else:
new_data = data
html = html[ 0:pos ] + new_data + html[ semicolon_pos+1:len( html )]
return html