Replace text BeautifulSoup

3

I have the following html:

<p>Um acidente no cruzamento da rua ... </p>
<div id="marca"></div>
<p>Um acidente no cruzamento ......</p>
<div id="marca2"></div>

I'm trying to do this:

def text_view(self):

    soup = BeautifulSoup(self.text)
    try:
        marca1 = BeautifulSoup(self.get_images())
        soup.find("div", {"id": "marca"}).replaceWith(marca1)
    except:
        pass

    try:
        marca2 = BeautifulSoup(self.get_images())
        soup.find("div", {"id": "marca2"}).replaceWith(marca2)
    except:
        pass

    return soup

But it only replaces the first div text. What can it be?

    
asked by anonymous 30.08.2014 / 15:51

1 answer

2

According to this question in SOen , when you insert a BeautifulSoup object into another object BeautifulSoup , find stops working correctly - it stops fetching before time (it's a bug in the find implementation). A workaround would do all searches first, and all replacements after:

soup = BeautifulSoup(self.text)

# Primeiro busca tudo
ponto1 = soup.find("div", {"id": "marca"})
ponto2 = soup.find("div", {"id": "marca2"})

# Depois substitui tudo
marca1 = BeautifulSoup(self.get_images())
marca2 = BeautifulSoup(self.get_images())

ponto1.replaceWith(marca1)
ponto2.replaceWith(marca2)

Other - as quoted in response to the linked question - would be to redo the object BeautifulSoup before doing the second find :

soup = BeautifulSoup(self.text)
try:
    marca1 = BeautifulSoup(self.get_images())
    soup.find("div", {"id": "marca"}).replaceWith(marca1)
except:
    pass

# Refaz o objeto
soup = BeautifulSoup(soup.renderContents())

try:
    marca2 = BeautifulSoup(self.get_images())
    soup.find("div", {"id": "marca2"}).replaceWith(marca2)
except:
    pass
    
30.08.2014 / 17:38