【问题标题】:How to avoid printing utf-8 characters in BeautifulSoup with replace_with如何避免在 BeautifulSoup 中使用 replace_with 打印 utf-8 字符
【发布时间】:2016-04-21 01:35:27
【问题描述】:

我遇到了问题,我可以找到解决方法。我正在尝试解析一个 html 页面,然后替换一个字符串,同时使用Beautiful Soup。虽然这个过程看起来是正确的,当我打开新的 html 页面时我没有收到任何错误,但里面有一些我不想要的 utf-8 字符。

工作代码示例:

#!/usr/bin/python

import codecs
from bs4 import BeautifulSoup

html_sample = """
<!DOCTYPE html>
<html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body>
<div class="date">LAST UPDATE</div>
</body>
</html>
"""

try:
    my_soup = BeautifulSoup(html_sample.decode('utf-8'), 'html.parser')  # html5lib or html.parser
    forecast = my_soup.find("div", {"class": "date"})
    forecast.tag = unicode(forecast).replace('LAST UPDATE', 'TEST')
    forecast.replace_with(forecast.tag)
    # print(my_soup.prettify())

    f = codecs.open('test.html', "w", encoding='utf-8')
    f.write(my_soup.prettify().encode('utf-8'))
    f.close()
except UnicodeDecodeError as e:
    print('Error, encoding/decoding: {}'.format(e))
except IOError as e:
    print('Error Replacing: {}'.format(e))
except RuntimeError as e:
    print('Error Replacing: {}'.format(e))

在新的 html 页面中输出 utf-8 字符:

<!DOCTYPE html>
<html>
 <head lang="en">
  <meta charset="utf-8">
   <meta content="width=device-width, initial-scale=1" name="viewport"/>
  </meta>
 </head>
 <body>
  &lt;div class="date"&gt;TEST&lt;/div&gt;
 </body>
</html>

我认为我混淆了编码和解码过程。在这方面有更多知识的人可以详细说明。我是编码和编码的初学者。

提前感谢您的时间和精力。

【问题讨论】:

  • 您正在用整个 forecast 元素的已处理字符串替换替换 forecast.tag。这与编码没有任何关系。
  • 如果我不这样做,应该由谁来做?这是我发现它有效的唯一方法。 :(

标签: python encoding utf-8 beautifulsoup


【解决方案1】:

这里没有必要进入编码。您可以通过设置element.string 来替换 Beautiful Soup 元素的文本内容,如下所示:

from bs4 import BeautifulSoup

html_sample = """
<!DOCTYPE html>
<html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body>
<div class="date">LAST UPDATE</div>
</body>
</html>
"""

soup = BeautifulSoup(html_sample)
forecast = soup.find("div", {"class": "date"})
forecast.string = 'TEST'
print(soup.prettify())

输出

<!DOCTYPE html>
<html>
 <head lang="en">
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1" name="viewport"/>
 </head>
 <body>
  <div class="date">
   TEST
  </div>
 </body>
</html>

【讨论】:

  • OMG 就是这么简单,我想不通。非常感谢您的回答。我的替代解决方案是创建一个副本并通过字符串替换过程格式化。但这是一个更好的解决方案。再次感谢。
  • 不用担心 - 如果有什么安慰的话,我还发现字符编码非常混乱!
  • 这两天我读了这么多文档,我想不通。你拯救了我的周末......:D
  • @Thanos:如果您发现 Unicode 令人困惑,您可能会发现这篇文章很有帮助:Pragmatic Unicode,由 SO 资深人士 Ned Batchelder 撰写。
  • 谢谢,看起来很有趣。我一定会花一些时间在上面。
猜你喜欢
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多