Есть ли способ удалить все теги html из строки, но оставить некоторые ссылки и изменить их представление? Пример:Python - Строка из тегов html, оставлять ссылки, но в измененной форме
description: <p>Animation params. For other animations, see <a href="#myA.animation">myA.animation</a> and the animation parameter under the API methods. The following properties are supported:</p>
<dl>
<dt>duration</dt>
<dd>The duration of the animation in milliseconds.</dd>
<dt>easing</dt>
<dd>A string reference to an easing function set on the <code>Math</code> object. See <a href="http://example.com">demo</a>.</dd>
</dl>
<p>
и я хочу, чтобы заменить
<a href="#myA.animation">myA.animation</a>
только 'myA.animation', но
<a href="http://example.com">demo</a>
с 'демо: http://example.com'
EDIT: Сейчас, похоже, работает:
def cleanComment(comment):
soup = BeautifulSoup(comment, 'html.parser')
for m in soup.find_all('a'):
if str(m) in comment:
if not m['href'].startswith("#"):
comment = comment.replace(str(m), m['href'] + " : " + m.__dict__['next_element'])
soup = BeautifulSoup(comment, 'html.parser')
comment = soup.get_text()
return comment
Ваши примеры глобальные правила для вас html? Или может быть, что некоторые ссылки вы хотите сохранить, а другие нет? – arieljannai
Да, есть только два типа ссылок. – Ratka