Tôi muốn tìm tất cả các bảng trong html bằng cách sử dụng BeautifulSoup. Bảng bên trong nên được bao gồm trong bảng bên ngoài.Tìm tất cả các bảng trong html bằng cách sử dụng BeautifulSoup
Tôi đã tạo một số mã hoạt động và nó cung cấp cho đầu ra dự kiến. Nhưng, tôi không thích giải pháp này, bởi vì nó phá hủy đối tượng 'canh'.
Bạn có biết cách làm điều đó một cách thanh lịch hơn không?
from BeautifulSoup import BeautifulSoup as bs
input = '''<html><head><title>title</title></head>
<body>
<p>paragraph</p>
<div><div>
<table>table1<table>inner11<table>inner12</table></table></table>
<div><table>table2<table>inner2</table></table></div>
</div></div>
<table>table3<table>inner3</table></table>
<table>table4<table>inner4</table></table>
</html>'''
soup = bs(input)
while(True):
t=soup.find("table")
if t is None:
break
print str(t)
t.decompose()
Output:
<table>table1<table>inner11<table>inner12</table></table></table>
<table>table2<table>inner2</table></table>
<table>table3<table>inner3</table></table>
<table>table4<table>inner4</table></table>