python 编码问题

ccjjww1222 2013-02-28 11:08:59
假如我想抓取一个网页的html,html编码为GBK

# coding=utf-8
import urllib.request

req = urllib.request.Request('http://www.xxxx.com/')
response = urllib.request.urlopen(req)
the_page = response.read()
print(the_page.decode())



输出错误:
File "D:\Apache2.2\cgi-bin\main.py", line 8, in <module>
print(the_page.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position 246: invalid start byte

我知道是编码的问题..
请大神指导一下


...全文
116 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccjjww1222 2013-02-28
  • 打赏
  • 举报
回复
thanks
李察德-泰森 2013-02-28
  • 打赏
  • 举报
回复

# coding=utf-8
import urllib.request
 
req = urllib.request.Request('http://www.xxxx.com/')
response = urllib.request.urlopen(req)
the_page = response.read().decode('gb2312')
print(the_page)
ccjjww1222 2013-02-28
  • 打赏
  • 举报
回复
网页中个含有中文..

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧