beautifulsoup 中的 renderContents (python)

发布于 2024-11-15 05:14:30 字数 400 浏览 0 评论 0原文

我试图工作的代码是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-11-22 05:14:30

您的错误消息和代码示例不一致。您说您正在调用:

heading.renderContents()

但您的错误消息显示您正在调用:

print h.renderContents()

这表明您的代码中可能存在错误,试图在不支持的字符串对象上调用 renderContents()定义该方法。

无论如何,如果您检查 heading 的对象类型以确保它确实是一个 BeautifulSoup 实例,将会有所帮助。这对我来说适用于 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文