Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion how to modify the text of a tag, without changing it's children ?

Received: by 10.224.188.8 with SMTP id cy8mr41942121qab.6.1341966286687;
        Tue, 10 Jul 2012 17:24:46 -0700 (PDT)
X-BeenThere: beautifulsoup@googlegroups.com
Received: by 10.229.105.85 with SMTP id s21ls8423qco.1.gmail; Tue, 10 Jul 2012
 17:24:45 -0700 (PDT)
Received: by 10.224.188.8 with SMTP id cy8mr41942082qab.6.1341966285767;
        Tue, 10 Jul 2012 17:24:45 -0700 (PDT)
Received: by 10.224.106.136 with SMTP id x8msqao;
        Tue, 10 Jul 2012 17:24:26 -0700 (PDT)
Received: by 10.52.179.98 with SMTP id df2mr3044754vdc.3.1341966265964;
        Tue, 10 Jul 2012 17:24:25 -0700 (PDT)
Date: Tue, 10 Jul 2012 17:24:25 -0700 (PDT)
From: bs4user <srehtva...@gmail.com>
To: beautifulsoup@googlegroups.com
Message-Id: <96f65f9a-eeb2-45ef-803d-2566ef3af7bf@googlegroups.com>
Subject: how to modify the text of a tag, without changing it's children ?
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1467_10952652.1341966265248"

------=_Part_1467_10952652.1341966265248
Content-Type: multipart/alternative; 
	boundary="----=_Part_1468_1624842.1341966265248"

------=_Part_1468_1624842.1341966265248
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

If a tag contains children, how can I change the text of the tag without 
changing or destroying the children?
 
e.g., in a case like:
 
<*tag1*> foo <*tag2*> abcdef <*tag3*> ghijkl </*tag3*></*tag2*></*tag1*>
 
how can I change "foo" to "bar" without changing anything else?
 
I cannot simply do:
     tag1.string = "bar"
because, according to the BS4 documentation "Be careful: if the tag 
contained other tags, they and all their contents will be destroyed."

------=_Part_1468_1624842.1341966265248
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<div>If a tag contains children, how can I change the text of the tag without changing or destroying the children?</div><div>&nbsp;</div><div>e.g., in a case like:</div><div>&nbsp;</div><div>&lt;<strong>tag1</strong>&gt;&nbsp;foo &lt;<strong>tag2</strong>&gt;&nbsp;abcdef &lt;<strong>tag3</strong>&gt;&nbsp;ghijkl &lt;/<strong>tag3</strong>&gt;&lt;/<strong>tag2</strong>&gt;&lt;/<strong>tag1</strong>&gt;</div><div>&nbsp;</div><div>how can I change "foo" to "bar" without changing anything else?</div><div>&nbsp;</div><div>I cannot simply do:</div><div>&nbsp;&nbsp;&nbsp;&nbsp; tag1.string = "bar"</div><div>because, according to the BS4 documentation "Be careful: if the tag contained other tags, they and all their contents will be destroyed."</div>
------=_Part_1468_1624842.1341966265248--

------=_Part_1467_10952652.1341966265248--