python3 urllib.request.urlopen() 地址打开错误

错误内容:UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in range(128)

1.以为是代码错误,检查tab符,并没有问题,

2.将代码粘贴到空白项目中去,发现还是不对。

3.百度:http://blog.csdn.net/olanlanxiari/article/details/48201231

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

  2.将上面问题自身检查,我这不存在这种问题,所以继续百度

  3.发现python3 urlopen()链接地址不能出现中文,,那么有中文怎么办?

    a.办法总是有的:替换

    b.将中文内容通过quote方法替换

    c.quote可用的参数如下:

      quote(string,safe='/',encoding=None,errors=None)

扫描二维码关注公众号,回复: 2516242 查看本文章

      reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","

代码:

from urllib.parse import quote
url = quote(url, safe=string.printable)

原文地址:

https://www.cnblogs.com/xiaochun365/p/6233014.html

https://blog.csdn.net/Kompany4/article/details/76408408

猜你喜欢

转载自blog.csdn.net/sunwukong_hadoop/article/details/81115427