Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62029
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrong,': 0.09; 'subject:Help': 0.11; "'replace'": 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'soup': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'subject:problem': 0.24; 'looks': 0.24; 'header:X -Complaints-To:1': 0.27; 'subject:please': 0.30; 'please.': 0.31; 'subject:the': 0.34; 'subject:with': 0.35; 'but': 0.35; 'to:addr :python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'break': 0.61; 'tag': 0.61; 'email addr:gmail.com': 0.63; 'subject:Need': 0.64 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Need Help with the BeautifulSoup problem, please |
| Date | Mon, 16 Dec 2013 10:33:24 +0100 |
| Organization | None |
| References | <89f67420-0b59-4f45-bf33-cd4e17467852@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Gmane-NNTP-Posting-Host | p5084b396.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4192.1387186374.18130.python-list@python.org> (permalink) |
| Lines | 37 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1387186374 news.xs4all.nl 2853 [2001:888:2000:d::a6]:49333 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:62029 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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