如何让 BeautifulSoup 将 textarea 标签的内容解析为 HTML?

bro*_*eld 5 python beautifulsoup html-parsing

在3.0.5之前,BeautifulSoup曾经将<textarea>的内容视为HTML。它现在将其视为文本。我正在解析的文档的 textarea 标记内有 HTML,我正在尝试处理它。

我试过了:

    for textarea in soup.findAll('textarea'):
        contents = BeautifulSoup.BeautifulSoup(textarea.contents)
        textarea.replaceWith(contents.html(text=True))
Run Code Online (Sandbox Code Playgroud)

但我收到错误。我在文档中找不到这个,并且替代解析器没有帮助。有人知道我如何将文本区域解析为 HTML 吗?

编辑:

HTML 示例为:

<textarea class="ks-lazyload-custom">
  <div class="product-view product-view-rug">
    Foobar Womble
    <div class="product-view-head">
      <img src="tps/i1/fo-25.gif" />
    </div>
  </div>
</textarea>
Run Code Online (Sandbox Code Playgroud)

错误是:

File "D:\src\cross\tserver\src\tools\sitecrawl\BeautifulSoup.py", line 1913, 
in _detectEncoding '^<\?.*encoding=[\'"](.*?)[\'"].*\?>').match(xml_data)
TypeError: expected string or buffer
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种获取元素、提取内容、使用 BeautifulSoup 解析它们、将其折叠为文本,然后用该文本替换原始元素的内容(或替换整个元素)的方法。

至于现实世界与规范,实际上在这里并不是特别相关。需要解析数据,我正在寻找这样做的方法。

Jus*_*eel 2

这似乎工作得相当好(如果我正确理解你想要什么):

for textarea in soup.findAll('textarea'):
    contents = BeautifulSoup.BeautifulSoup(textarea.contents[0]).renderContents()
    textarea.replaceWith(contents)
Run Code Online (Sandbox Code Playgroud)