beautifulsoup 中的 renderContents (python)
我试图工作的代码是:
h = str(heading)
# '<h1>Heading</h1>'
heading.renderContents()
我收到此错误:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print h.renderContents()
AttributeError: 'str' object has no attribute 'renderContents'
有什么想法吗?
我有一个带有 html 标签的字符串,我需要清理它,如果有不同的方法,请建议它。
The code I'm trying to get working is:
h = str(heading)
# '<h1>Heading</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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误消息和代码示例不一致。您说您正在调用:
但您的错误消息显示您正在调用:
这表明您的代码中可能存在错误,试图在不支持的字符串对象上调用
renderContents()
定义该方法。无论如何,如果您检查
heading
的对象类型以确保它确实是一个 BeautifulSoup 实例,将会有所帮助。这对我来说适用于 BeautifulSoup 3.2.0:Your error message and your code sample don't line up. You say you're calling:
But your error message says you're calling:
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: