Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62029

Re: Need Help with the BeautifulSoup problem, please

From Peter Otten <__peter__@web.de>
Subject Re: Need Help with the BeautifulSoup problem, please
Date 2013-12-16 10:33 +0100
Organization None
References <89f67420-0b59-4f45-bf33-cd4e17467852@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4192.1387186374.18130.python-list@python.org> (permalink)

Show all headers | View raw


seaspeak@gmail.com wrote:

>     I need to replace all tag <b> with <span> after ■. But the result from
>     below is '■   <span style="REPLACE">D</span> / <font></font>'
> Can you explain what I did wrong, please.
> 
>     s = '■<b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
>     soup = BeautifulSoup(s)
>     for i in soup.find_all(text='■'):
>         tag = soup.new_tag('span')
>         tag['style'] = 'REPLACE'
>         for ii in i.find_next_siblings():
>             if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
>                 break
>             else:
>                 if ii.name=='b':
>                     tag.string=ii.string
>                     print(ii.replace_with(tag))
>     print(soup)

It looks like you cannot reuse a tag. Try

    s = '■<b>A</b> <b>B</b> <b>C</b> <b>D</b> / <font></font>'
    soup = BeautifulSoup(s)
    for i in soup.find_all(text='■'):
        for ii in i.find_next_siblings():
            if ii.name=='font' or str(ii).lstrip('')[0:1]=='/':
                break
            else:
                if ii.name=='b':
                    tag = soup.new_tag('span')
                    tag['style'] = 'REPLACE'
                    tag.string=ii.string
                    print(ii.replace_with(tag))
    print(soup)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Need Help with the BeautifulSoup problem, please seaspeak@gmail.com - 2013-12-15 22:41 -0800
  Re: Need Help with the BeautifulSoup problem, please 88888 Dihedral <dihedral88888@gmail.com> - 2013-12-16 00:02 -0800
    Re: Need Help with the BeautifulSoup problem, please seaspeak@gmail.com - 2013-12-16 01:26 -0800
  Re: Need Help with the BeautifulSoup problem, please seaspeak@gmail.com - 2013-12-16 00:39 -0800
  Re: Need Help with the BeautifulSoup problem, please Peter Otten <__peter__@web.de> - 2013-12-16 10:33 +0100
  Re: Need Help with the BeautifulSoup problem, please Andreas Perstinger <andipersti@gmail.com> - 2013-12-16 10:32 +0100

csiph-web