Reputation: 93
table = soup.find('table', attrs={'id':'MainContent_grdUsers2'})
data = []
for tr in table.find_all('tr')[1:] :
td = tr.find_all('td')
try :
data += [
[
td[0].getText() ,
td[2].find('option', {'selected':'selected'}).getText(),
td[3].find('option', {'selected':'selected'}).getText(),
td[4].find('input').get('value'),
td[5].find('input').get('value'),
td[6].find('option', {'selected':'selected'}).getText()
]
]
except Exception as ex :
print(ex)
continue
with open('test5.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
for row in data :
writer.writerow([' '.join(str(r) for r in row)])
I am trying to parse a complex html table to csv. This code works and grabs the desired data but each row is contained in one cell instead of separate columns for each of the 6 values per row. What am I doing wrong here?
Upvotes: 0
Views: 448
Reputation: 6302
You are unnecessarily setting newline=''
and joining the table cells with spaces. Your data appears to be in a suitable format to be passed directly to writer.writerows()
.
with open('test5.csv', 'w') as outfile:
writer = csv.writer(outfile)
header = ['Name', 'Rights', 'Bureaus', 'FullName', 'Email', 'Status']
writer.writerow(header)
writer.writerows(data)
Upvotes: 1