Python BeautifulSoup4合并table单元格


 1     from bs4 import BeautifulSoup 
 2 
 3     str_table = """<table><tr><td>456</td><td>123</td></tr><tr><td>123</td><td>456</td></tr><tr><td>123</td><td>456</td><td>789</td></tr><tr><td>456</td><td>123</td></tr></table>"""
 4     soup = BeautifulSoup(str_table, "html.parser")
 5     row_span = 1
 6     for tr in soup.find_all('tr'):
 7         tds = tr.find_all('td')
 8         next_trs = tr.next_siblings
 9         for next_tr in next_trs:
10             if tds[0].get_text() == next_tr.contents[0].get_text():
11                 row_span += 1
12                 tds[0]["rowspan"] = row_span
13                 next_tr.contents[0].extract()
14             else:
15                 row_span = 1
16                 break
17     print(soup.prettify())

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM