0
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)])
我想將一個複雜的html表格解析爲csv。此代碼工作並獲取所需的數據,但每行包含在一個單元中,而不是每行6個值中的每一個的單獨列。我在這裏做錯了什麼?將由Beautifulsoup解析的數據拆分爲csv列
如果可能的話,包含輸入數據的樣本總是一個好主意。我假設您使用的是http://stackoverflow.com/q/43396370/2997179中的相同數據? –