Need Help with the BeautifulSoup problem, please

seaspeak at gmail.com seaspeak at gmail.com
Mon Dec 16 04:26:42 EST 2013


88888 Dihedral於 2013年12月16日星期一UTC+8下午4時02分42秒寫道:
> On Monday, December 16, 2013 2:41:08 PM UTC+8, seas... at 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)
> 
> 
> 
> I think you should try some descent 
> 
> free editors such as notepad++ 
> 
> for your source codes to 
> 
> replace trivial strings without 
> 
> programmig.

I think it's my fault, thanks



More information about the Python-list mailing list