0

I think I'm missing something basic. I'd like to access the text of an XML element and replace it. For example, if I have this:
<name>Fred</name> I'd like to be able to change Fred to John

I've read a lot of sites about ElementTree and BeautifulSoup but I'm still stuck. Could someone provide a very simple example?

3 Answers 3

1

Like this?

  1. use BeautifulSoup's soup.find() method to find the HTML tag:

    >>> from bs4 import BeautifulSoup
    >>> BeautifulSoup('<html><body><name>Fred</name><html><body>')
    >>> soup = BeautifulSoup('<html><body><name>Fred</name><html><body>')
    >>> name = soup.find('name')
    >>> name
    <name>Fred</name>
    >>> 
    
  2. use tag.string = newstring to replace the string of it:

    >>> name.string = 'John'
    >>> name
    <name>John</name>
    >>> soup
    <html><body><name>John</name><html><body></body></html></body></html>
    >>> 
    

Then we done, check the document for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

A python2.7 version of beautiful soup

from BeautifulSoup import BeautifulSoup
soup=BeautifulSoup("<name>Fred</name>")
soup.find("name").string="John"
print(soup)

output

<name>John</name>

Alternative using regex

import re
htmltext="<name>John</name>"
new_htmltext=re.sub("(<name>).*(</name>)","\\1Fred\\2",htmltext)
print(new_htmltext)

Comments

0

Why not just parse it as text? The format of the data doesn't always necessitate the use of a special library for working with it.

>>> a = 'stuff<name>Fred</name>otherstuff'
>>> a.replace('<name>Fred</name>', '<name>John</name>')
'stuff<name>John</name>otherstuff'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.