鱼C论坛

 找回密码
 立即注册
查看: 2077|回复: 2

[已解决]53课问题求解

[复制链接]
发表于 2017-8-9 21:25:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为甚么53课课后作业最后一题我编码hao123,youku ,163等的编码后,文本是这样

而鱼c官网的编码正常

111

111
最佳答案
2017-8-10 10:27:46
本帖最后由 shinemic 于 2017-8-10 10:34 编辑

首先把问题复现。为了方便起见,我写了个函数,直接把「解码后」的字符串写入文件:
  1. import urllib.request

  2. fishC = 'http://www.fishC.com'
  3. hao123 = 'http://www.hao123.com'
  4. youku = 'http://www.youku.com'
  5. netbase = 'http://www.163.com'

  6. def write_html_to_file(url, filename, decode = 'utf-8'):
  7.     response = urllib.request.urlopen(url)
  8.     html = response.read()
  9.     html = html.decode(decode)
  10.     fstream = open(filename + '.html', 'w')
  11.     fstream.write(html)
  12.     fstream.close
复制代码

进行试验:
  1. write_html_to_file(fishC, 'fishC')
  2. write_html_to_file(hao123, 'hao123')
  3. write_html_to_file(youku, 'youku')
  4. write_html_to_file(netbase, 'netbase')
复制代码
试验其中的 fishC,hao123,youku都没有问题,但网易这里开始报错:
  1. ---------------------------------------------------------------------------
  2. UnicodeDecodeError                        Traceback (most recent call last)
  3. <ipython-input-17-7b03f968a2d0> in <module>()
  4.      11     fstream.close
  5.      12
  6. ---> 13 write_html_to_file(netbase, 'netbase')

  7. <ipython-input-17-7b03f968a2d0> in write_html_to_file(url, filename, decode)
  8.       6     response = urllib.request.urlopen(url)
  9.       7     html = response.read()
  10. ----> 8     html = html.decode(decode)
  11.       9     fstream = open(filename + '.html', 'w')
  12.      10     fstream.write(html)

  13. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 565: invalid continuation byte
复制代码
百度谷歌一通,发现是网页编码的问题。打开网易,F12打开开发者控制台(Chrome 浏览器),发现有这么一句话:
  1. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
复制代码
说明网页以 gbk 进行编码的. 所以我写的函数参数里面有个 decode 参数, 方便后来更改:
  1. write_html_to_file(netbase, 'netbase', 'gbk')
复制代码
这样一来就没有问题了(netbase.html文件节选):
  1. <!DOCTYPE HTML>
  2. <!--[if IE 6 ]> <html class="ne_ua_ie6 ne_ua_ielte8" id="ne_wrap"> <![endif]-->
  3. <!--[if IE 7 ]> <html class="ne_ua_ie7 ne_ua_ielte8" id="ne_wrap"> <![endif]-->
  4. <!--[if IE 8 ]> <html class="ne_ua_ie8 ne_ua_ielte8" id="ne_wrap"> <![endif]-->
  5. <!--[if IE 9 ]> <html class="ne_ua_ie9" id="ne_wrap"> <![endif]-->
  6. <!--[if (gte IE 10)|!(IE)]><!--> <html phone="1" id="ne_wrap"> <!--<![endif]-->
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  9. <meta name="model_url" content="http://www.163.com/special/0077rt/index.html" />
  10. <title>网易</title>
  11. <base target="_blank" />
  12. <meta name="Keywords" content="网易,邮箱,游戏,新闻,体育,娱乐,女性,亚运,论坛,短信,数码,汽车,手机,财经,科技,相册" />
  13. <meta name="Description" content="网易是中国领先的互联网技术公司,为用户提供免费邮箱、游戏、搜索引擎服务,开设新闻、娱乐、体育等30多个内容频道,及博客、视频、论坛等互动交流,网聚人的力量。" />
复制代码
但是如果直接双击这个文件,发现是乱码:
ex_53_mess.png

当然这属于浏览器 / 前端的问题,不是 Python 的讨论范围内了,修改方法是把网页编码改为 UTF-8 即可:
ex_53_after.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-9 23:18:18 | 显示全部楼层
终端下的错误显示是:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 565: invalid continuation byte
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-10 10:27:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 shinemic 于 2017-8-10 10:34 编辑

首先把问题复现。为了方便起见,我写了个函数,直接把「解码后」的字符串写入文件:
  1. import urllib.request

  2. fishC = 'http://www.fishC.com'
  3. hao123 = 'http://www.hao123.com'
  4. youku = 'http://www.youku.com'
  5. netbase = 'http://www.163.com'

  6. def write_html_to_file(url, filename, decode = 'utf-8'):
  7.     response = urllib.request.urlopen(url)
  8.     html = response.read()
  9.     html = html.decode(decode)
  10.     fstream = open(filename + '.html', 'w')
  11.     fstream.write(html)
  12.     fstream.close
复制代码

进行试验:
  1. write_html_to_file(fishC, 'fishC')
  2. write_html_to_file(hao123, 'hao123')
  3. write_html_to_file(youku, 'youku')
  4. write_html_to_file(netbase, 'netbase')
复制代码
试验其中的 fishC,hao123,youku都没有问题,但网易这里开始报错:
  1. ---------------------------------------------------------------------------
  2. UnicodeDecodeError                        Traceback (most recent call last)
  3. <ipython-input-17-7b03f968a2d0> in <module>()
  4.      11     fstream.close
  5.      12
  6. ---> 13 write_html_to_file(netbase, 'netbase')

  7. <ipython-input-17-7b03f968a2d0> in write_html_to_file(url, filename, decode)
  8.       6     response = urllib.request.urlopen(url)
  9.       7     html = response.read()
  10. ----> 8     html = html.decode(decode)
  11.       9     fstream = open(filename + '.html', 'w')
  12.      10     fstream.write(html)

  13. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 565: invalid continuation byte
复制代码
百度谷歌一通,发现是网页编码的问题。打开网易,F12打开开发者控制台(Chrome 浏览器),发现有这么一句话:
  1. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
复制代码
说明网页以 gbk 进行编码的. 所以我写的函数参数里面有个 decode 参数, 方便后来更改:
  1. write_html_to_file(netbase, 'netbase', 'gbk')
复制代码
这样一来就没有问题了(netbase.html文件节选):
  1. <!DOCTYPE HTML>
  2. <!--[if IE 6 ]> <html class="ne_ua_ie6 ne_ua_ielte8" id="ne_wrap"> <![endif]-->
  3. <!--[if IE 7 ]> <html class="ne_ua_ie7 ne_ua_ielte8" id="ne_wrap"> <![endif]-->
  4. <!--[if IE 8 ]> <html class="ne_ua_ie8 ne_ua_ielte8" id="ne_wrap"> <![endif]-->
  5. <!--[if IE 9 ]> <html class="ne_ua_ie9" id="ne_wrap"> <![endif]-->
  6. <!--[if (gte IE 10)|!(IE)]><!--> <html phone="1" id="ne_wrap"> <!--<![endif]-->
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  9. <meta name="model_url" content="http://www.163.com/special/0077rt/index.html" />
  10. <title>网易</title>
  11. <base target="_blank" />
  12. <meta name="Keywords" content="网易,邮箱,游戏,新闻,体育,娱乐,女性,亚运,论坛,短信,数码,汽车,手机,财经,科技,相册" />
  13. <meta name="Description" content="网易是中国领先的互联网技术公司,为用户提供免费邮箱、游戏、搜索引擎服务,开设新闻、娱乐、体育等30多个内容频道,及博客、视频、论坛等互动交流,网聚人的力量。" />
复制代码
但是如果直接双击这个文件,发现是乱码:
ex_53_mess.png

当然这属于浏览器 / 前端的问题,不是 Python 的讨论范围内了,修改方法是把网页编码改为 UTF-8 即可:
ex_53_after.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-1 10:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表