Skip to content

Instantly share code, notes, and snippets.

@eyJhb
Last active January 31, 2019 14:20
Show Gist options
  • Save eyJhb/b592aafd9f5d05623164687c25299ea8 to your computer and use it in GitHub Desktop.
Save eyJhb/b592aafd9f5d05623164687c25299ea8 to your computer and use it in GitHub Desktop.
Transpose HTML table
from bs4 import BeautifulSoup
f = open("index.html", "r")
data = f.read()
f.close()
bs = BeautifulSoup(data, "html.parser")
elements = {}
services = []
headers = []
first = True
for row in bs.findAll("tr"):
if first:
first = False
columns = row.findAll("th")
header = columns[0].getText()
headers.append(header)
elements[header] = []
for x in range(1,15):
column = str(columns[x])
column = column[5:-6]
elements[header].append(column)
# print(column)
print(len(services))
continue
# exit()
columns = row.findAll("td")
header = columns[0].getText()
headers.append(header)
elements[header] = []
for x in range(1,15):
try:
elements[header].append(columns[x].getText().strip())
except:
elements[header].append("")
print(len(elements))
print(headers)
print(elements)
print("-----")
print('<table class="centered">')
print("\t"+'<tr>')
for header in headers:
print("\t\t"+'<th>'+header+'</th>')
print("\t"+'</tr>')
for x in range(0,14):
print("\t"+'<tr>')
# print("\t\t"+'<td>'+services[x]+'</td>')
for header in headers:
print("\t\t"+'<td>'+elements[header][x]+'</td>')
print("\t"+'</tr>')
print('</table>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment