Beautiful Soup Error: '' Object Has No Attribute 'contents'?
I'm writing a script that extracts the content out of an article and removes any unnecessary stuff eg. scripts and styling. Beautiful Soup keeps raising the following exception: '&
Solution 1:
I think the problem is that in remove_all_elements
or somewhere else in your code you are deleting the contents
attribute of some of your tags.
It looks like this is happening when you call to_remove.decompose()
. Here is the source for that method:
defdecompose(self):
"""Recursively destroys the contents of this tree."""
self.extract()
i = self
while i isnotNone:
next = i.next_element
i.__dict__.clear()
i = next
Here is what happens if you call this function manually:
>> soup = BeautifulSoup('<div><p>hi</p></div>')
>>>d0 = soup.find_all('div')[0]>>>d0
<div><p>hi</p></div>
>>>d0.decompose()>>>d0
Traceback (most recent call last):
...
Traceback (most recent call last):
AttributeError: '<class 'bs4.element.Tag'>' object has no attribute 'contents'
It appears that once you have called decompose
on a tag you must never attempt to use that tag again. I'm not quite sure where this is happening though.
One thing I would try checking is that len(element.__dict__) > 0
at all times in your trim()
function.
Post a Comment for "Beautiful Soup Error: '' Object Has No Attribute 'contents'?"