Modifying string contents

In the preceding example, we added the two div tags without any contents to the HTML tree. We need to add the corresponding string in the name property of the div tag and also into the number property of the div tag. This can also be done in multiple ways using Beautiful Soup.

Using .string to modify the string content

We can use the .string attribute of a tag to modify the string content. So, we can add or modify the string value using the following code:

new_div_name_tag.string="phytoplankton"
print(producer_entries.prettify())

#output
<ul id="producers">
  <li class="producerlist">
    <div class="name">
      plants
    </div>
    <div class="number">
      100000
    </div>
  </li>
  <li class="producerlist">
    <div class="name">
      algae
    </div>
    <div class="number">
      100000
    </div>
  </li>
  <li class="producerlist">
    <div class="name">
      phytoplankton
    </div>
    <div class="number">
    </div>
  </li>
</ul>

We can see that the string has been added in the preceding code example.

Adding strings using .append(), insert(), and new_string()

We can add more strings to the existing tag using the .append() or .insert() method. They behave in the same way as in adding a new tag. In the case of string contents, the append() method appends to the end of .string, whereas the insert() method inserts to the specific position of .string. For example, we can add a new string to the name property of the div tag using the following code:

new_div_name_tag.append("producer")
print(soup.prettify())

#output
<html>
  <body>
    <div class="ecopyramid">
      <ul id="producers">
        <li class="producerlist">
          <div class="name">
            plants
          </div>
          <div class="number">
            100000
          </div>
        </li>
        <li class="producerlist">
          <div class="name">
            algae
          </div>
          <div class="number">
            100000
          </div>
        </li>
        <li class="producerlist">
          <div class="name">
            phytoplankton
            producer
          </div>
          <div class="number">
          </div>
        </li>
      </ul>
    </div>
  </body>
</html>

There is one more method, new_string(), that will help in creating a new string as follows:

new_string_toappend = soup.new_string("producer")
new_div_name_tag.append(new_string_toappend)

The preceding code will create the new string and now, we can use either append() or insert() to add the newly created string to the tree.

Like append(), we can also use insert() for inserting strings as follows:

new_string_toinsert  =soup.new_string("10000")
new_div_number_tag.insert(0,new_string_toinsert)

The resulting tree after the addition of the producer will look like the following code:

<html>
  <body>
    <div class="ecopyramid">
      <ul id="producers">
        <li class="producerlist">
          <div class="name">
            plants
          </div>
          <div class="number">
            100000
          </div>
        </li>
        <li class="producerlist">
          <div class="name">
            algae
          </div>
          <div class="number">
            100000
          </div>
        </li>
        <li class_="producerlist">
          <div class_="name">
            phytoplankton
            producer
          </div>
          <div class="number">
            10000
          </div>
        </li>
      </ul>
    </div>
  </body>
</html>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.16.23