remove highlighting <b>

72 views
Skip to first unread message

janwillem

unread,
Oct 31, 2009, 5:54:30 PM10/31/09
to beautifulsoup
I have entered a html file into a beautyfulsoup. I now want to remove
all highlighting by <b>...</b> before print soup.prettyfy(). How can I
do that? Thanks, Janwillem

Aaron DeVore

unread,
Nov 1, 2009, 2:16:53 AM11/1/09
to beauti...@googlegroups.com
Janwillem,
My Beautiful Soup mod has a new method of Tag called
replaceWithChildren. It does exactly what it sounds like; it removes
an element from the tree and inserts its children where it was just
at.

for tag in soup.findAll"b"):
tag.replaceWithChildren()

The URL is: http://github.com/adevore/old-beautiful-soup

Cheers!
Aaron DeVore

Aaron DeVore

unread,
Nov 1, 2009, 4:24:57 PM11/1/09
to beauti...@googlegroups.com
Just as a warning, there is an issue with how replaceWithChildren
handles text nodes. It doesn't merge child text nodes with surrounding
nodes. If you have this structure

<p>spam <b>eggs</b> cheese </p>

You will end up with this tree
p
"spam"
"eggs"
"cheese"

As opposed to
p
"spam eggs cheese"

By the way, if you happen to run into any bugs I would like to hear about it.

-Aaron DeVOre

janwillem

unread,
Nov 1, 2009, 3:28:28 PM11/1/09
to beautifulsoup
Thanks for your reply. However, I am afraid that my understanding of
the BeautyfulSoup together with too little experience in html makes
that it does not work for me. My test fragment, based on a fragment of
a Google Scholar page, is:
paper = '<span class="a">author1, <b>author2</b>, author3 - Title of
paper, year - Publisher</span>'
soup = BeautifulSoup(''.join(paper))
for tag in soup.findAll('b'):
print tag.prettify()
tag.replaceWithChildren()
print tag.prettify()
print '-----'

This results in:
<b>
author2
</b>

Traceback (most recent call last):
File "testbeauty.py", line 18, in <module>
tag.replaceWithChildren()
TypeError: 'NoneType' object is not callable

If I delete the () of replaceWithChildren I get:
<b>
author2
</b>

<b>
author2
</b>

-----

This makes that soup.prettify() gives
<span class="a">
author1,
<b>
author2
</b>
, author3 - Title of paper, year - Publisher
</span>

and what I want is
<span class="a">
author1, author2, author3 - Title of paper, year - Publisher
</span>

How to achieve?
Cheers, Janwillem

On Nov 1, 8:16 am, Aaron DeVore <aaron.dev...@gmail.com> wrote:
> Janwillem,
> My Beautiful Soup mod has a new method of Tag called
> replaceWithChildren. It does exactly what it sounds like; it removes
> an element from the tree and inserts its children where it was just
> at.
>
> for tag in soup.findAll"b"):
>     tag.replaceWithChildren()
>
> The URL is:http://github.com/adevore/old-beautiful-soup
>
> Cheers!
> Aaron DeVore
>

Aaron DeVore

unread,
Nov 1, 2009, 11:37:11 PM11/1/09
to beauti...@googlegroups.com
It looks like you're not using my modified version. Check to make sure
that you're not importing the official Beautiful Soup.

-Aaron

janwillem

unread,
Nov 1, 2009, 3:28:55 PM11/1/09
to beautifulsoup
> Janwillem,
> My Beautiful Soup mod has a new method of Tag called
> replaceWithChildren. It does exactly what it sounds like; it removes
> an element from the tree and inserts its children where it was just
> at.
>
> for tag in soup.findAll"b"):
>     tag.replaceWithChildren()
>
> The URL is:http://github.com/adevore/old-beautiful-soup
>
> Cheers!
> Aaron DeVore
>
Reply all
Reply to author
Forward
0 new messages