1

Below is a simple html segment to parse with beautifulsoup4 and I hope to extract the top level raw text hello.

mysoup = BeautifulSoup('<td>hello<script type="text/javascript">world</script></td>')

And I've tried several intuitive ways but without expected results:

mysoup.text            # u'helloworld'
mysoup.contents        # [<html><body><td>hello<script type="text/javascript">world</script></td></body></html>]
list(mysoup.strings)   # [u'hello ', u'world']

So how to achieve this goal?

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85

1 Answers1

0

First, get a reference to the td node. Then, iterate through its children and see which of them are strings:

from bs4 import BeautifulSoup
mysoup = BeautifulSoup('<td>hello<script type="text/javascript">world</script></td>')
td = mysoup.find('td')
print [s for s in td.children if isinstance(s, basestring)]
Community
  • 1
  • 1
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137