Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'else:': 0.03; 'received:192.168.178': 0.07; 'wrong,': 0.09; 'subject:Help': 0.11; "'replace'": 0.16; 'andreas': 0.16; 'bye,': 0.16; 'received:192.168.178.20': 0.16; 'soup': 0.16; 'wrote:': 0.18; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'subject:problem': 0.24; 'url:moin': 0.24; 'header:In-Reply-To:1': 0.27; 'subject:please': 0.30; 'url:wiki': 0.31; 'please.': 0.31; 'url:python': 0.33; 'subject:the': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'accessing': 0.36; 'url:org': 0.36; 'list.': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'read': 0.60; 'break': 0.61; 'tag': 0.61; 'new': 0.61; 'simply': 0.61; 'email addr:gmail.com': 0.63; 'subject:Need': 0.64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=SHf9TlDXH0IZ8fImvjzIjPUzuSQla6VfY8Fw86jiMDE=; b=0hsgkRP2bGIJr1YdrWKP6de1hut5sHp+FxGyHPATdBXb3VtywjJa126NPU7/hpZB3D UxRMhUnfv8MqrKv0sffBwiXDeXc9jMaymcODj2XRWIEJKjf3egOFowJvSXaS3MlNMa6Z KNk2lrhm6XI2Al7miCA5LAb1DtAyA0X8NcJGWvrZnTNHid/W++dgxgQ4Eown91rmcjty HEZK1E9BlcmiRYyCOPa1JjlQj1r9Sm5QXGMw5nTQe656Usgv23etQouQGPLQMe0TdVyE KJJQDglvhSXnMS/fdDbB+DbegqAhTzwsxStgqaLYgMgFTXKervna99eUT/mozIu1NDzg 9lpw== X-Received: by 10.14.114.71 with SMTP id b47mr15870674eeh.26.1387186333707; Mon, 16 Dec 2013 01:32:13 -0800 (PST) Date: Mon, 16 Dec 2013 10:32:10 +0100 From: Andreas Perstinger User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Need Help with the BeautifulSoup problem, please References: <89f67420-0b59-4f45-bf33-cd4e17467852@googlegroups.com> In-Reply-To: <89f67420-0b59-4f45-bf33-cd4e17467852@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387186340 news.xs4all.nl 2977 [2001:888:2000:d::a6]:48097 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62030 On 16.12.2013 07:41, seaspeak@gmail.com wrote: > I need to replace all tag with after ■. But the result > frombelow is '■ D / ' > Can you explain what I did wrong, please. > > s = '■A B C D / ' > 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