def parse_armor_table(table, table_data): soup = BeautifulSoup(table['body']) fields = parse_armor_header(soup.table.thead.tr) armor_list = [] armor_type = "" for line in soup.table.contents: if line.name == 'thead': armor_type = line.tr.contents[0].getText().strip() elif line.name == 'tr': if len(line.contents) == 1: armor_type = line.getText().strip() else: armor = {'Armor Type': armor_type} for i in range(len(line.contents)): armor[fields[i]] = clear_nbsp(line.contents[i].getText()) add_additional_fields(table_data, armor) armor_list.append(armor) return process_armor(table_data, armor_list)
def parse_gear_table(table, table_data): soup = BeautifulSoup(table['body']) gear = [] gear_type = "" modifier = None for line in soup.table.contents: if line.name == 'thead': gear_type, fields = header_function(line) elif line.name == 'tr': if len(line.contents) == 1: modifier = line.getText().strip() else: item = {'Gear Type': gear_type} if modifier_clear_function(modifier, line): modifier = None for i in range(len(line.contents)): item[fields[i]] = clear_nbsp(line.contents[i].getText()) if modifier and not item['Name'].startswith(modifier): item['Name'] = "%s, %s" % (modifier, item['Name']) add_additional_fields(table_data, item) gear.append(item) return process_gear(table_data, gear)
def parse_gear_table(table, table_data): soup = BeautifulSoup(table['body']) gear = [] gear_type = "" modifier = None for line in soup.table.contents: if line.name == 'thead': gear_type, fields = header_function(line) elif line.name == 'tr': if len(line.contents) == 1: modifier = line.getText().strip() else: item = {'Gear Type': gear_type} if modifier_clear_function(modifier, line): modifier = None for i in range(len(line.contents)): item[fields[i]] = clear_nbsp( line.contents[i].getText()) if modifier and not item['Name'].startswith(modifier): item['Name'] = "%s, %s" % (modifier, item['Name']) add_additional_fields(table_data, item) gear.append(item) return process_gear(table_data, gear)
def parse_weapon_table(table, table_data): soup = BeautifulSoup(table['body']) fields = parse_weapon_header(soup.table.thead.tr) category = None weapon_class = None weapons = [] for line in soup.table.contents: if line.name == 'thead': category = line.tr.contents[0].getText() elif line.name == 'tr': if len(line.contents) == 1: weapon_class = line.getText() else: weapon = {'Proficiency':category, 'Weapon Class': weapon_class} for i in range(len(line.contents)): data = line.contents[i].getText() if data != '—': weapon[fields[i]] = clear_nbsp( line.contents[i].getText()) elif fields[i] in ('Cost', 'Weight'): weapon[fields[i]] = line.contents[i].getText() add_additional_fields(table_data, weapon) weapons.append(weapon) return process_weapons(table_data, weapons)