如何只获取BeautifulSoup中标签的内部文本,不包括嵌入式标签?

Pra*_*nav 2 screen-scraping urllib2 beautifulsoup web-scraping python-requests

例如,

<ul>
    <li>
        <b>Hey, sexy!</b>
        Hello
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我只想要li标签中的"Hello" .

如果我使用soup.find("ul").li.text它也包括b标签.

Pau*_*ney 5

你可以使用这样的find功能

from bs4 import BeautifulSoup

html = '''<ul><li><b>Hey, sexy!</b>Hello</li></ul>'''
soup = BeautifulSoup(html)
print soup.find('li').find(text=True, recursive=False)
Run Code Online (Sandbox Code Playgroud)