用python抓取编码为gb2312的网页,结果抓取的都是乱码 怎样才能将它弄成正常的HTML格式?

data = urllib.request.openurl(url)data = data.decode(✀gb2312✀,✀ignore✀).encode(✀utf-8✀)
2025-04-28 18:09:40
推荐回答(1个)
回答1:

你试试下面的代码

#!/usr/bin/env python
# -*- coding:utf8 -*-

import urllib2

req = urllib2.Request("http://www.baidu.com/")
res = urllib2.urlopen(req)
html = res.read()
res.close()

html = unicode(html, "gb2312").encode("utf8")
print html