【问题标题】:renderContents in beautifulsoup (python)美丽汤中的渲染内容(python)
【发布时间】:2011-06-15 05:07:21
【问题描述】:

我正在尝试使用的代码是:

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 标签的字符串,如果有其他方法,我需要清理它,请提出建议。

【问题讨论】:

    标签: python html beautifulsoup


    【解决方案1】:

    您的错误消息和您的代码示例没有对齐。你说你在打电话:

    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>
    

    【讨论】:

      猜你喜欢
      • 2015-12-25
      • 1970-01-01
      • 2016-03-22
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多