Urllib是Python提供的一个用于操做URL的模块,在Python2.X中,有Urllib库,也有Urllib2库,在Python3.X中Urllib2合并到了Urllib中,咱们爬取网页的时候,常常须要用到这个库。下面总结了Urllib相关模块中从Python2.X到Python3.X的常见的一些变更。
在Python2.X中使用import urllib2——对应的,在Python3.X中会使用import urllib.request,urllib.error。python
在Python2.X中使用import urllib——对应的,在Python3.X中会使用import urllib.request,urllib.error,urllib.parse。cookie
在Python2.X中使用import urlparse——对应的,在Python3.X中会使用import urllib.parse。url
在Python2.X中使用import urllib2——对应的,在Python3.X中会使用import urllib.request,urllib.error。code
在Python2.X中使用urllib2.urlopen——对应的,在Python3.X中会使用urllib.request.urlopen。blog
在Python2.X中使用urllib.urlencode——对应的,在Python3.X中会使用urllib.parse.urlencode。class
在Python2.X中使用urllib.quote——对应的,在Python3.X中会使用urllib.request.quote。import
在Python2.X中使用cookielib.CookieJar——对应的,在Python3.X中会使用http.CookieJar。request
在Python2.X中使用urllib2.Request——对应的,在Python3.X中会使用urllib.request.Request
im