将Beautifulsoup解析的数据拆分为csv列

时间:2017-04-18 14:44:11

标签: python-3.x csv beautifulsoup

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个值中的每一个都包含单独的列。我在这做错了什么?

1 个答案:

答案 0 :(得分:1)

您不必要地设置newline=''并使用空格连接表格单元格。您的数据似乎采用合适的格式直接传递给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)