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"> ...
Get Getting Started with Beautiful Soup now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.