开发者

renderContents in beautifulsoup (python)

The code I'm trying to get working is:

h = str(heading)
# '<h1>Headi开发者_StackOverflowng</h1>'
heading.renderContents()

I get this error:

Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print h.renderContents()
AttributeError: 'str' object has no attribute 'renderContents'

Any ideas?

I have a string with html tags and i need to clean it if there is a different way of doing that please suggest it.


Your error message and your code sample don't line up. You say you're calling:

heading.renderContents()

But your error message says you're calling:

print h.renderContents()

Which suggests that perhaps you have a bug in your code, trying to call renderContents() on a string object that doesn't define that method.

In any case, it would help if you checked what type of object heading is to make sure it's really a BeautifulSoup instance. This works for me with BeautifulSoup 3.2.0:

from BeautifulSoup import BeautifulSoup
heading = BeautifulSoup('<h1>heading</h1>')
repr(heading)
# '<h1>heading</h1>'
print heading.renderContents()
# <h1>heading</h1>
print str(heading)
# '<h1>heading</h1>'
h = str(heading)
print h
# <h1>heading</h1>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜