代码内容:

url = 'https://movie.douban.com/j/search_subjects?type=movie'+ str(tag) + '&sort=recommend&page_limit=20&page_start=' + str(limit)
response = urllib.request.urlopen(url, timeout=20)
result = response.read().decode('utf-8','ignore').replace(u'\xa9', u'')
result = json.loads(result)

  

错误内容为:上述第二行代码报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in range(128)

1 认为是代码错误,或者是tab缩进错误

2 百度搜索后得出如下分析:

Python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一般设置为utf8的编码格式。

但是在我使用的python3.6.5 默认就是utf8编码格式,所以也不存在这种问题。我使用print(type(str))后输出的也是str。

3 发现python3 urlopen()链接地址中不能出现中文,而上述代码的tag是传入的中文字符,终于找到了问题的所在。

解决办法:

使用urllib.parse.quote进行转换。

url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=' + str(tag) + '&sort=recommend&page_limit=20&page_start=' + str(limit)
url = quote(url, safe=string.printable)
response = urllib.request.urlopen(url, timeout=20)
result = response.read().decode('utf-8','ignore').replace(u'\xa9', u'')
result = json.loads(result)

  

程序头部需要添加

from urllib.parse import quote

同时需要import string导入string模块

方法quote的参数safe表示可以忽略的字符。

string.printable表示ASCII码第33~126号可打印字符,其中第48~57号为0~9十个阿拉伯数字;65~90号为26个大写英文字母,97~122号为26个小写英文字母,其余的是一些标点符号、运算符号等。

如果去掉safe参数的内容将会出错。
---------------------
作者:xjtu帽帽
来源:CSDN
原文:https://blog.csdn.net/qq_25406563/article/details/81253347
版权声明:本文为博主原创文章,转载请附上博文链接!

(转)python3 urllib.request.urlopen() 错误UnicodeEncodeError: 'ascii' codec can't encode characters的更多相关文章

  1. 【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1

    转自:https://blog.csdn.net/AckClinkz/article/details/78538462 环境 >>> import sys >>> ...

  2. (转) Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1

    (转)python(三):Python3-UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1 python ...

  3. 解决Python2.7的UnicodeEncodeError:'ascii' codec can't encode characters in position 0-78: ordinal not in range(128)异常错误

    解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误 大家都知道,在使用python进行网络爬虫时,最头疼的就是转码问题,下面是 ...

  4. Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)

    在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal no ...

  5. 【转】Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)

    [转]Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128) ...

  6. [错误解决]UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

    python2内容无法写入csv,报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordin ...

  7. UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-15: ordinal not in range(128)

    python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报类似这样的错误. UnicodeEncodeError: 'ascii' codec can't ...

  8. UnicodeEncodeError: 'ascii' codec can't encode characters in position

    UnicodeEncodeError: 'ascii' codec can't encode characters in position python运行时出现这个错误,解决方法如下: 加入如下语句 ...

  9. 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position

    最近用Python写了些爬虫,在爬取一个gb2312的页面时,抛出异常: UnicodeEncodeError: 'ascii' codec can't encode characters in po ...

随机推荐

  1. springboot自定义静态文件目录,解决jar打包后修改页面等静态文件的问题

    1.问题 springboot开发时候,一般将文件放在resources目录,但是发布后想修订文件或是开发时候修改了文件内容一般需重新打包或者重启动才能达到效果: 2.原因 将资源文件打包入jar包, ...

  2. 我的菜单在母版页,如何更改菜单点击后的效果 Ver2

    很久之前,Insus.NET使用ASP.NET实现一个功能,非javascript.<我的菜单在母版页,如何更改菜单点击后的效果>http://www.cnblogs.com/insus/ ...

  3. C# 字符串首字符大写

    我找到一些把字符串首字符大写的方法. 假如需要把字符串 "red" 转换为 "Red",把 "red house" 转为 "Red ...

  4. nodejs 的序列化与反序列化

    1.序列化 stringify函数的作用就是序列化对象,也就是说将对象类型转换成一个字符串类型(默认的分割符("&")和分配符("=")),先介绍它的基 ...

  5. JavaScriptDay3

    js对标签的操作 创建标签:document.createElement("tag"); 便签添加内容 : document.createElement("tag&quo ...

  6. 积分之迷-2015决赛C语言B组第一题

    标题:积分之迷 小明开了个网上商店,卖风铃.共有3个品牌:A,B,C. 为了促销,每件商品都会返固定的积分. 小明开业第一天收到了三笔订单: 第一笔:3个A + 7个B + 1个C,共返积分:315 ...

  7. 安装apr-1.6.3报错[cannot remove `libtoolT’: No such file or directory]解决方法

    发现有这个提示:cannot remove `libtoolT’: No such file or directory , 编辑 configure文件,查找 $RM "$cfgfile&q ...

  8. HDU3592(差分约束)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. PDO异常处理

    PDO提供了三种处理错误的方式 PDO::ERRMODE_SILENT:静默模式(默认) PDO::ERRMODE_WARNING:警告模式 PDO::ERRMODE_EXCEPTION:异常模式 示 ...

  10. css语法和基本知识

    1.CSS全称为“层叠样式表”,它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. 注:使用CSS样式的一个好处是通过定义某个样式,可以让不同网页位置的文字有着统一的字体 ...