zoukankan      html  css  js  c++  java
  • 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")
  • 相关阅读:
    梦断代码阅读笔记03
    用户场景分析
    学习进度8
    学习进度7
    梦断代码阅读笔记02
    学习进度6
    随堂小测app(nabcd)
    梦断代码阅读笔记01
    《构建之法》-6
    《构建之法》-5
  • 原文地址:https://www.cnblogs.com/satty/p/9876394.html
Copyright © 2011-2022 走看看