使用Beautiful Soup清除标签内的内容。

huangapple go评论73阅读模式
英文:

Clear tag inner content with Beautiful Soup

问题

You can achieve your goal by first decoding the HTML entities and then using BeautifulSoup to manipulate the text. Here's the modified code:

from bs4 import BeautifulSoup
import html

text = '''Your HTML text here'''

# Decode HTML entities
decoded_text = html.unescape(text)

# Create a BeautifulSoup object
soup = BeautifulSoup(decoded_text, 'html.parser')

# Find the code tag and remove its contents
code_tag = soup.find('code')
if code_tag:
    code_tag.clear()

# Extract text from other tags
result = soup.get_text()

result

This code will decode HTML entities, remove the content within the code tag, and extract the text from the other tags.

英文:

i have a text like this:

<p>In an article talking about ResNet, there has the following statement</p>\n\n<p><strong>The second, the bottleneck unit, consists of three stacked operations. A series of 1x1, 3x3 and 1x1 convolutions substitute the previous design. The two 1x1 operations are designed for reducing and restoring dimensions. This leaves the 3x3 convolution, in the middle, to operate on a less dense feature vector. Also, BN is applied after each convolution and before ReLU non-linearity.</strong></p>\n\n<p>I am not clear how to understand the statement of <code>This leaves the 3x3 convolution, in the middle, to operate on a less dense feature vector.</code>What does that mean? In specific, what does the <code>less feature vector</code> mean, what causes the generation of this <code>less dense feature vector</code>?</p>\n

I want to remove everything between "code" tag and then extract text from the other tags.

I tried to use

soup = BeautifulSoup(text, 'html.parser')
tag = soup.code
tag. Clear()
tag 

but I've got: <code></code> as output. Why isn't the rest of text removed?

答案1

得分: 1

Just use .extract().

For example:

from bs4 import BeautifulSoup

soup = BeautifulSoup(sample_html, 'html.parser')
for code_tag in soup.find_all('code'):
    code_tag.extract()

print(soup.getText(strip=True))
print(f"Removed characters from <code> tags: {len(sample_html) - len(soup.getText(strip=True))}")

This, based on your sample, should give you:

In an article talking about ResNet, there has the following statementThe second, the bottleneck unit, consists of three stacked operations. A series of 1x1, 3x3 and 1x1 convolutions substitute the previous design. The two 1x1 operations are designed for reducing and restoring dimensions. This leaves the 3x3 convolution, in the middle, to operate on a less dense feature vector. Also, BN is applied after each convolution and before ReLU non-linearity.I am not clear how to understand the statement ofWhat does that mean? In specific, what does themean, what causes the generation of this?

Removed characters from <code> tags: 222
英文:

Just use .extract().

For example:

from bs4 import BeautifulSoup

soup = BeautifulSoup(sample_html, &#39;html.parser&#39;)
for code_tag in soup.find_all(&#39;code&#39;):
    code_tag.extract()

print(soup.getText(strip=True))
print(f&quot;Removed characters from &lt;code&gt; tags: {len(sample_html) - len(soup.getText(strip=True))}&quot;)

This, based on your sample, should give you:

In an article talking about ResNet, there has the following statementThe second, the bottleneck unit, consists of three stacked operations. A series of 1x1, 3x3 and 1x1 convolutions substitute the previous design. The two 1x1 operations are designed for reducing and restoring dimensions. This leaves the 3x3 convolution, in the middle, to operate on a less dense feature vector. Also, BN is applied after each convolution and before ReLU non-linearity.I am not clear how to understand the statement ofWhat does that mean? In specific, what does themean, what causes the generation of this?

Removed characters from &lt;code&gt; tags: 222

huangapple
  • 本文由 发表于 2023年5月7日 03:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定