简体   繁体   English

如何只返回HTML代码段中的文本?

[英]How to return just the text from a HTML snippet?

I have a HTML snippet that looks like this: 我有一个HTML代码段,如下所示:

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

I want to strip all tags and just have the text. 我想剥离所有标签,只需要文本。

Content should look like this: 内容应如下所示:

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

I'm looking for something like this: 我在寻找这样的东西:

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

You're looking for get_text() : 你正在寻找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
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM