背景
线上运行的一些比较老的Python2脚本,由于种种原因,需要升级成Python3环境运行,由于Python2和Python3的不兼容,因此需要对脚本内容进行修改之后,才可以正常运行,本文记录关于升级过程中,关于urllib相关的修改点及遇到的问题和解决方案。
问题记录
问题:POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
解决方案:参数需要进行编码, urlencode语句后加 encode(encoding=‘utf-8’)
问题:urllib has no attribute request
解决方案:import urllib.request
参考链接:https://stackoverflow.com/questions/37042152/python-3-5-1-urllib-has-no-attribute-request
问题:AttributeError: module ‘urllib’ has no attribute ‘parse’
解决方案:同上,import urllib.parse
参考链接:https