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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -