【发布时间】: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>
<div class="date">TEST</div>
</body>
</html>
我认为我混淆了编码和解码过程。在这方面有更多知识的人可以详细说明。我是编码和编码的初学者。
提前感谢您的时间和精力。
【问题讨论】:
-
您正在用整个
forecast元素的已处理字符串替换替换forecast.tag。这与编码没有任何关系。 -
如果我不这样做,应该由谁来做?这是我发现它有效的唯一方法。 :(
标签: python encoding utf-8 beautifulsoup