-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_parser.py
36 lines (29 loc) · 1.25 KB
/
character_parser.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
from bs4 import BeautifulSoup
import re
from json import dumps
from datetime import datetime
def replace_unicode(u_string):
return u_string.encode('utf-8').replace("\xa0", " ").replace("\xc2", "")
def parse_single_char(plain_html):
#Create BeautifulSoup Object
soup = BeautifulSoup(plain_html)
#all the important information is in 3 seperate tables
tables = soup.findAll('table')
char_dict = {}
for table in tables:
# general character information is in this table
# print table
tr_list = table.findAll('tr')
if tr_list[0].text == "Character Information":
char_information_table = table
for elem in char_information_table.findAll('tr'):
if elem.text != "Character Information":
a = elem.findAll('td')
char_dict[replace_unicode(a[0]\
.text[:-1]\
.lower())\
.replace(" ", "_")] = replace_unicode(a[1].text)
char_dict['level'] = int(char_dict['level'])
char_dict['achievement_points'] = int(char_dict['achievement_points'])
#char_dict['last_login'] = datetime.strptime(char_dict['last_login'], '%b %d %Y, %H:%M:%S %Z')
return char_dict