How to change string content of a node that has child elements?

Senff1389

I'm trying to make a script in Python using BeautifulSoup where the text on the whole page is going to be changed into something else.

So far it's going good, but I'm having trouble whenever I encounter a node that has both a string and another node inside it.

As an example, here is some sample HTML:

   <div>
        abc
        <p>xyz</p>
   </div>

What I want to do is change the "abc" part of the HTML without affecting the remaining content of the node.

As you probably already know, using element.string in BeautifulSoup only works with nodes that have one child element, and since in this example the <div> node has two children (text and the <p> tag), trying to access the string attribute is going to end with a Runtime Error, saying that NoneType has no string attribute.

Is there a way to go around using the string attribute and changing the text portion of a node in this specific scenario?

Andrej Kesely

You can access various contents of the <div> tag with .contents property and then use .replace_with() to put new text there:

from bs4 import BeautifulSoup

html_doc = '''\
<div>
    abc
    <p>xyz</p>
</div>'''

soup = BeautifulSoup(html_doc, 'html.parser')

soup.div.contents[0].replace_with('\n    Hello World\n    ')
print(soup)

Prints:

<div>
    Hello World
    <p>xyz</p>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

find if xml string node has any child elements or not in jquery

How to change the child content of a div?

How do I add a Class to Specific Parent Elements ONLY if the Child Container has Content

How to cancel waitpid if child has no status change?

how to change all elements id in div child

How to change child property of parent elements?

How to change the order of ancestors of flex child elements

How to change the class of elements that have matching content

godot - how to change sprite of a child node dynamically

Change content of text elements in svg on a node.js server

How to add Parent node if Child node elements are equal?

Remove all parent node's content if any child node has no value

How to remove elements that directly contain a string - ignoring child elements

How to check if an HTML Element has on the of child elements as a link

Laravel count how many child elements has every row

Determining if a child div has content

How to make child content of a node uneditable dynamically in ProseMirror?

How to change content of the String Linked List

How to change a string based on its content in Python

How to get child node value which has namespace on it?

How to instantly change the visibility property of child if it has a transition

How can I change the width of a RelativeLayout that has a GridLayout as child?

How to change a word on a line that has a certain string on it

How to get elements that have at least one direct child text node

How to concatenate child elements under one parent node by | separator in XSLT?

How I can change child elements order in JS?

wrapping child node content in xquery

How to change the texture of a content that has been loaded already?

How to change html content, when the page has fully loaded