【发布时间】:2016-07-28 13:25:55
【问题描述】:
Beautifulsoup 对于在 python 中解析 html 很方便,但我遇到了问题,就是使用干净的代码直接使用 string 或 text 获取值
from bs4 import BeautifulSoup
tr ="""
<table>
<tr><td>text1</td></tr>
<tr><td>text2<div>abc</div></td></tr>
</table>
"""
table = BeautifulSoup(tr,"html.parser")
for row in table.findAll("tr"):
td = row.findAll("td")
print td[0].text
print td[0].string
结果:
text1
text1
text2abc
None
我怎样才能得到结果
text1
text2
我想跳过多余的内部标签
beautifulsoup4-4.5.0 与python 2.7 一起使用
【问题讨论】:
-
td[0].contents[0]应该给你你所追求的。
标签: python beautifulsoup html-parsing