在beautifulsoup4中具有子元素的标记内提取文本节点

use*_*812 6 python beautifulsoup web-scraping

我正在解析和抓取的HTML具有以下代码:

<li> <span> 929</span> Serve Returned </li>
Run Code Online (Sandbox Code Playgroud)

<li>在这种情况下,我如何只提取"服务返回" 的文本节点Beautifulsoup

.string因为<li>有一个子元素,所以不起作用,并.text返回里面的文本<span>.

Tot*_*tem 2

str.replace为此使用了以下方法:

>>> li = soup.find('li') # or however you need to drill down to the <li> tag 
>>> mytext = li.text.replace(li.find('span').text, "") 
>>> print mytext
Serve Returned
Run Code Online (Sandbox Code Playgroud)