如何在 BeautifulSoup 中呈现 unicode 标签的内容?

发布于 2024-07-20 04:52:14 字数 463 浏览 1 评论 0原文

这是来自 WordPress 帖子详细信息页面的汤:

content = soup.body.find('div', id=re.compile('post'))
title = content.h2.extract()
item['title'] = unicode(title.string)
item['content'] = u''.join(map(unicode, content.contents))

我想在分配 item['content'] 时省略封闭的 div 标签。 有没有办法以 unicode 形式呈现一个标签的所有子标签? 比如:

item['content'] = content.contents.__unicode__()

这会给我一个单一的 unicode 字符串而不是一个列表。

This is a soup from a WordPress post detail page:

content = soup.body.find('div', id=re.compile('post'))
title = content.h2.extract()
item['title'] = unicode(title.string)
item['content'] = u''.join(map(unicode, content.contents))

I want to omit the enclosing div tag when assigning item['content']. Is there any way to render all the child tags of a tag in unicode? Something like:

item['content'] = content.contents.__unicode__()

that will give me a single unicode string instead of a list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

金橙橙 2024-07-27 04:52:14

您是否尝试过:

unicode(content)

它将 content 的标记转换为单个 Unicode 字符串。

编辑:如果您不需要封闭标签,请尝试:

content.renderContents()

Have you tried:

unicode(content)

It converts content's markup to a single Unicode string.

Edit: If you don't want the enclosing tag, try:

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