Python 如何删除特定类的标记?
我正在使用Beautifulsoup(python3.x)解析HTML页面 我正在尝试从我编写的标记中获取数据Python 如何删除特定类的标记?,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我正在使用Beautifulsoup(python3.x)解析HTML页面 我正在尝试从我编写的标记中获取数据 def getBody(url): html_page = requests.get(url) soup = BeautifulSoup(html_page.content, 'html.parser') Con = "".join([p.text for p in soup.find_all("p")]) #print(Con) return Con
def getBody(url):
html_page = requests.get(url)
soup = BeautifulSoup(html_page.content, 'html.parser')
Con = "".join([p.text for p in soup.find_all("p")])
#print(Con)
return Con
但在这样做的过程中,我从下面的htmltag获得了文本。我怎样才能删除这个
本文的评论已关闭。
您可以使用或删除标记
>>> from bs4 import BeautifulSoup
>>> html = '''
... <p>text</p>
... <p class="notice">Comments are closed for this article.</p>
... <p>text</p>
... <p class="notice">Comments are closed for this article.</p>
... <p>text</p>'''
>>> soup = BeautifulSoup(html, 'html.parser')
>>> for tag in soup.find_all('p', class_='notice'):
... tag.decompose()
...
>>> soup
<p>text</p>
<p>text</p>
<p>text</p>
>>来自bs4导入组
>>>html=“”
... 正文
... 此文章的评论已关闭
... 正文
... 此文章的评论已关闭
... 文本“”
>>>soup=BeautifulSoup(html,'html.parser')
>>>用于汤中的标记。查找所有('p',class='notice'):
... tag.decompose()
...
>>>汤
正文
正文
正文