【问题标题】:BeautifulSoup is HTML escaping strings which have escaped charactersBeautifulSoup 是具有转义字符的 HTML 转义字符串
【发布时间】:2012-11-08 14:59:05
【问题描述】:

我正在从文件中读取一个字符串:

a = '<script>closedSign: \'<img src="/static/images/drop-down.png" style="margin-top: -3px;"  />\'</script>'

现在,当我跑步时

BeautifulSoup(a)

<script>closedSign: '&lt;img src="/static/images/drop-down.png" style="margin-top: -3px;"   /&gt;'</script>

因此,&amp;lt;img 被 HTML 转义为 &amp;lt;img

我怎样才能避免这种情况?

【问题讨论】:

  • 避免什么?你想达到什么目标?
  • 刚刚编辑了问题以更好地解释它。另外,刚刚注意到这个问题只发生在 BeautifulSoup3.2.1 而不是 3.2.0
  • 听起来你遇到了这个问题:bugs.launchpad.net/beautifulsoup/+bug/949074

标签: python escaping beautifulsoup html-escape-characters


【解决方案1】:

使用 BeautifulSoup 3.2.0 而不是 3.2.1 来解决这个问题。

【讨论】:

  • 也为我解决了这个问题。非常感谢您的解决方案,感谢 BeautifulSoup 在一次小更新后打破这样的事情。
【解决方案2】:

查看Beautiful Soup Documentation的“实体转换”部分。

soup = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)

【讨论】:

  • Paulo,convertEntities=BeautifulSoup.HTML_ENTITIES 适用于已经被 HTML 转义的字符串。示例:从解析 & lt;img 中返回
  • 如果在脚本标签 HTML 上再次运行 BeautifulSoup 会发生什么?
  • 对于可能的bs4 用户:convertEntities doesn't 在 bs4 中不再存在。
猜你喜欢
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
相关资源
最近更新 更多