Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除特定类的标记?_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 如何删除特定类的标记?

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

我正在使用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
但在这样做的过程中,我从下面的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() ... >>>汤 正文

正文

正文