4 个答案:
答案 0 :(得分:1)
from bs4 import BeautifulSoup
# Open test.html for reading
with open('test.html') as html_file:
soup = BeautifulSoup(html_file.read(), features='html.parser')
# Go through each 'A' tag and replace text with '-'
for tag in soup.find_all(id='A'):
tag.string.replace_with('-')
# Store prettified version of modified html
new_text = soup.prettify()
# Write new contents to test.html
with open('test.html', mode='w') as new_html_file:
new_html_file.write(new_text)
其中提供以下test.html:
- |
B |
C |
D |
答案 1 :(得分:0)
在没有任何库的情况下使用Python ,您可以使用以下代码将包含A的行替换为所需的行,我只