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")
  • 相关阅读:
    char 转string
    博客,记忆的图谱。
    history
    openstack Icehouse发布
    数据库常用命令
    nagios
    screen
    openstack 流量控制
    sublime 3
    磁盘类型
  • 原文地址:https://www.cnblogs.com/satty/p/9876394.html
Copyright © 2011-2022 走看看