1

我有一个如下所示的 HTML 片段:

<pre>zdfsfsf<br/>adfadfadf
adfadfasdfadfad  adfadf adf 
Mill Valley, CA 94941
122-2323-24124
Email: adfadfadf<br/><i>sfsfsfsf</i></pre>
<br/>

我想去掉所有标签,只剩下文字。

内容应如下所示:

zdfsfsf adfadfadf
adfadfasdfadfad  adfadf adf 
Mill Valley, CA 94941
122-2323-24124
Email: adfadfadf sfsfsfsf

我正在寻找这样的东西:

cells = row.find_all('td')
for c in cells:
    c.STRIP_HTML_TAGS()?????? <--WHAT IS THIS FUNCTION?
4

1 回答 1

3

您正在寻找get_text()

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup("""<pre>zdfsfsf<br/>adfadfadf
... adfadfasdfadfad  adfadf adf 
... Mill Valley, CA 94941
... 122-2323-24124
... Email: adfadfadf<br/><i>sfsfsfsf</i></pre>
... <br/>""")
>>> print(soup.get_text())
zdfsfsfadfadfadf
adfadfasdfadfad  adfadf adf 
Mill Valley, CA 94941
122-2323-24124
Email: adfadfadfsfsfsfsf
>>> 
于 2013-06-20T01:36:55.510 回答