2

From this HTML code:

<p class="description" dir="ltr">Name is a fine man. <br></p>

I'm looking for replacing "Name" using the following code:

target = soup.find_all(text="Name")
for v in target:
    v.replace_with('Id')

The output I would like to have is:

<p class="description" dir="ltr">Id is a fine man. <br></p>

When I:

print target
[]

Why doesn't it find the "Name"?

Thanks!

5
  • try using str.replace(), Are you sure you have some text that matches it? Commented Jul 4, 2015 at 11:41
  • You have no Element with Text Name. Show your HTML, and what you intend to do. Commented Jul 4, 2015 at 11:50
  • Not really that clear what you're asking. Firstly did your soup find any text='Name'? Also is there any code in between replace_with and print target? Commented Jul 4, 2015 at 11:51
  • @Daniel, I edited the question including the HTML. Commented Jul 4, 2015 at 11:59
  • @PaulRooney, apparently my soup did not find text='Name' and there is no code in between, Commented Jul 4, 2015 at 12:03

2 Answers 2

8

The text node in your HTML contains some other text besides "Name". In this case, you need to relax search criteria to use contains instead of exact match, for example, by using regex. Then you can replace matched text nodes with the original text except for "Name" part should be replaced with "Id" by using simple string.replace() method, for example :

from bs4 import BeautifulSoup
import re

html = """<p class="description" dir="ltr">Name is a fine man. <br></p>"""
soup = BeautifulSoup(html)
target = soup.find_all(text=re.compile(r'Name'))
for v in target:
    v.replace_with(v.replace('Name','Id'))
print soup

output :

<html><body><p class="description" dir="ltr">Id is a fine man. <br/></p></body></html>
Sign up to request clarification or add additional context in comments.

Comments

1

It returns an empty list because searching for text like this must match the whole text in a tag, so use regular expression instead.

From the official docs: BeautifulSoup - Search text

text is an argument that lets you search for NavigableString objects instead of Tags. Its value can be a string, a regular expression, a list or dictionary, True or None, or a callable that takes a NavigableString object as its argument:

soup.findAll(text="one")
# [u'one']
soup.findAll(t ext=re.compile("paragraph"))
# [u'This is paragraph ', u'This is paragraph ']
soup.findAll(text=lambda(x): len(x) < 12)
# [u'Page title', u'one', u'.', u'two', u'.']

P.S.: Already already discussed answers are here and here.

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.