Я использую этот код для сохранения тега <p>
и <br>
в строке.
from bs4 import BeautifulSoup
mystring = 'aaa<p>Radio and<BR> television.<br></p><p align="right">very<br/> popular in the world today.</p><p class="myclass">Millions of people watch TV. </p><p>That’s because a radio is very small <span style=":_black;">98.2%</span></p><p>and it‘s easy to carry. <span style=":_black;">haha100%</span></p>bb'
soup = BeautifulSoup(mystring,'html.parser')
for e in soup.find_all():
if e.name not in ['p','br']:
e.unwrap()
print(str(soup))
Результат:
aaa<p>Radio and<br/> television.<br/></p><p align="right">very<br> popular in the world today.</br></p><p class="myclass">Millions of people watch TV. </p><p>That’s because a radio is very small 98.2%</p><p>and it‘s easy to carry. haha100%</p>bb
Но я обнаружил, что есть некоторые атрибутыпод тегом <p>
.Например, выровнять и класс.На самом деле я хочу удалить align="right"
и class="myclass"
и другие атрибуты в теге <p>
, просто оставьте тег <p>
.
Мне нужен этот результат:
aaa<p>Radio and<br/> television.<br/></p><p>very<br> popular in the world today.</br></p><p>Millions of people watch TV. </p><p>That’s because a radio is very small 98.2%</p><p>and it‘s easy to carry. haha100%</p>bb
Я хочу удалить атрибуты под тегом <p>
.
Как это сделать?