【发布时间】:2017-09-12 16:00:31
【问题描述】:
我正在解析表格中的一些段落。
这是内容和代码。
txt = '''
<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head><table><tr><th filter=all>Employee Name</th><th filter=all>Project Name</th><th filter=all>Area</th><th filter=all>Date</th><th filter=all>Employee Manager</th></tr>
<tr><td style="vnd.ms-excel.numberformat:@">David</td><td style="vnd.ms- excel.numberformat:@">Review-2016</td><td style="vnd.ms- excel.numberformat:@">US</td><td align=right>17/03/2016</td><td style="vnd.ms- excel.numberformat:@">Andrew</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Kate</td><td style="vnd.ms-excel.numberformat:@">Review 2016</td><td style="vnd.ms-excel.numberformat:@">UK</td><td align=right>21/03/2016</td><td style="vnd.ms-excel.numberformat:@">Liz</td></tr>
'''
soup = BeautifulSoup(txt, "lxml")
soup.prettify()
list_5 = soup.find_all('table')[0].find_all("tr")
for row in list_5:
for nn in row.find_all("td"):
print nn.text
到目前为止,文本已经得到但都放在一起,即:
David
Review-2016
US
17/03/2016
Andrew
Kate
Review 2016
UK
21/03/2016
Liz
需要的是列形式,例如 David、Kate 或 US、UK 等。
你能帮我正确的方法吗?谢谢。
【问题讨论】:
标签: python beautifulsoup