G
G
god_dan632021-11-25 16:00:43
Python
god_dan63, 2021-11-25 16:00:43

How to change the content of a tag in an html document?

Hey!
I want to change the value of the tag, which is in the html document.
Here is a piece of html document:

<tr>\n
          <td class="highlight-grey" data-highlight-colour="grey"><strong>Содержание требования</strong></td>\n
          <td colspan="4"><span style="color: rgb(59,29,0);">Вот этот текст нужно поменять на нужный мне.</span></td>
        </tr>\n
        <tr>\n
          <td class="highlight-grey" data-highlight-colour="grey"><strong>Описание требования</strong></td>\n
          <td colspan="4">
            <br />
          </td>
        </tr>\n

I know that you can change it through "Tag.string", but after trying to understand examples of its use, I didn't understand how it works.

I can find my tag in the html dock itself, using bs4, but what about changing it?
<span style="color: rgb(59,29,0);">Вот этот текст нужно поменять на нужный мне.</span>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-11-25
@god_dan63

html.replace("Вот этот текст нужно поменять на нужный мне.", "Нужный текст")

Or find the tag through BS4 and change its text, which is in the field string:
soup.find('span').string = 'new string'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question