python - BeautifulSoap to get first value using string/text -
beautifulsoup handy html parsing in python, meet problem have clean code value directly using string or 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") row in table.findall("tr"): td = row.findall("td") print td[0].text print td[0].string result:
text1 text1 text2abc none how can result for
text1 text2 i want skip inner tag
beautifulsoup4-4.5.0 used python 2.7
you try this:
for row in table.findall("tr"): td = row.findall("td") t = td[0] print t.contents[0] but work if looking text before div tag
Comments
Post a Comment