[Tutor] Delete an attribute using Beautiful Soup?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Nov 22 21:26:21 CET 2005



On Tue, 22 Nov 2005, Bob Tanner wrote:

> Bob Tanner wrote:
>
> > I'd like to delete an attribute of  tag, the BGCOLOR to the BODY tag to be
> > exact.
>
> I believe I answered my own question. Looking for confirmation this is the
> "right" solution.
>
> del(soup.body['bgcolor'])

Yes, del should do it.


del is the deletion statement that lets us do things like remove keys from
a dictionary:

######
>>> states = {'TX' : 'Texas',
...           'CA' : 'California',
...           'PL' : 'Pluto'}
######


If we want to get rid of an intergalactic mistake, we can use del:

######
>>> del states['PL']
>>> states
{'CA': 'California', 'TX': 'Texas'}
######

Good luck!



More information about the Tutor mailing list