gpt4 book ai didi

python - 使用 mechanize 和 Python 2.6 下载 HTML 的编码问题

转载 作者:太空狗 更新时间:2023-10-30 02:35:15 25 4
gpt4 key购买 nike

browser = mechanize.Browser()
page = browser.open(url)
html = page.get_data()

print html

它显示了一些奇怪的字符。我想它是 UTF-8 字符串,但 Python 不知道并且无法正确显示它。

如何将这个字符串转换成unicode字符串

u = u'test'

最佳答案

压缩包

def ungzipResponse(r,b):
headers = r.info()
if headers['Content-Encoding']=='gzip':
import gzip
gz = gzip.GzipFile(fileobj=r, mode='rb')
html = gz.read()
gz.close()
headers["Content-type"] = "text/html; charset=utf-8"
r.set_data( html )
b.set_response(r)

response = browser.open(url)
ungzipResponse(response, browser)
html = response.read()

关于python - 使用 mechanize 和 Python 2.6 下载 HTML 的编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3804572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com