21
commentary = soup.find('div', {'id' : 'live-text-commentary-wrapper'})
findtoure = commentary.find(text = re.compile('Gnegneri Toure Yaya')).replace('Gnegneri      Toure Yaya', 'Yaya Toure')

Commentary contains various instances of Gnegneri Toure Yaya that need changing to Yaya Toure.

findAll() doesn't work as findtoure is a list.

The other problem I have is this code simply finds them and replaces them into a new variable called findtoure, I need to replace them in the original soup.

I think I am just looking at this from the wrong perspective.

1
  • @MartijnPieters I hope you had a Beautiful Soup for dinner ;), but I can't blame you if you don't remember. Commented Dec 16, 2014 at 20:15

1 Answer 1

29

You cannot do what you want with just .replace(). From the BeautifulSoup documentation on NavigableString:

You can’t edit a string in place, but you can replace one string with another, using replace_with().

That's exactly what you need to do; take each match, then call .replace() on the contained text and replace the original with that:

findtoure = commentary.find_all(text = re.compile('Gnegneri Toure Yaya'))
for comment in findtoure:
    fixed_text = comment.replace('Gnegneri Toure Yaya', 'Yaya Toure')
    comment.replace_with(fixed_text)

If you want to use these comments further, you'll need to do a new find:

findtoure = commentary.find_all(text = re.compile('Yaya Toure'))

or, if you all you need is the resulting strings (so Python str objects, not NavigableString objects still connected to the BeautifulSoup object), just collect the fixed_text objects:

findtoure = commentary.find_all(text = re.compile('Gnegneri Toure Yaya'))
fixed_comments = []
for comment in findtoure:
    fixed_text = comment.replace('Gnegneri Toure Yaya', 'Yaya Toure')
    comment.replace_with(fixed_text)
    fixed_comments.append(fixed_text)
Sign up to request clarification or add additional context in comments.

3 Comments

The unicode statement gives an error. What package is that from ? does it work in 3.7+ ?
@blissweb: this was using Python 2 syntax. I've updated it for Python 3.
how can I print the new text in the loop?

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.