解决脚本兼容urllib和urllib2(python2和python3)

  • A+
所属分类:python语言

解决脚本兼容urlliburllib2python2python3

有时候在写叫脚本的时候遇到python23的问题,有时候很头疼

下面是解决python中的urllib使用的方法,提供一个参考

 
 

try:

from urllib.request import urlopen, Request # Python 3

except ImportError:

from urllib2 import urlopen, Request # Python 2

头像

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: