urllib和urllib2在python2以及python3的區別


1.python2

import urllib

import urllib2

共同點:都可以直接用urlopen(‘url’)請求頁面

不同點:

urllib有urlencode(dict)和unquote()進行編碼和解碼

對於error:

try:
    response = urllib2.urlopen("http://pythonsite.com/111341.html")
except urllib2.HTTPError as e:
    print(e.reason)
    print(e.code)
    print(e.headers)
except urllib2.URLError as e:
    print(e.reason)

else:
    print("reqeust successfully")

2.python3

請求頁面:urllib.request.urlopen(‘url’)

對於error:

from urllib import request,error try: response = request.urlopen("http://pythonsite.com/113211.html") except error.HTTPError as e: print(e.reason) print(e.code) print(e.headers) except error.URLError as e: print(e.reason) else: print("reqeust successfully")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM