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


Groups > comp.lang.python > #62030

Re: Need Help with the BeautifulSoup problem, please

Date 2013-12-16 10:32 +0100
From Andreas Perstinger <andipersti@gmail.com>
Subject Re: Need Help with the BeautifulSoup problem, please
References <89f67420-0b59-4f45-bf33-cd4e17467852@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4191.1387186340.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 16.12.2013 07:41, seaspeak@gmail.com wrote:
> I need to replace all tag <b> with <span> after ■. But the result
> frombelow 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)
>

You are only creating one new tag but as I understand your problem you 
want to replace each b-element with a new tag. Simply move the tag 
creating part:

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))

And please read
https://wiki.python.org/moin/GoogleGroupsPython
if you want to continue using Google Groups for accessing this list.

Bye, Andreas

Back to comp.lang.python | Previous | NextPrevious 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