英文:
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, '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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论