zoukankan      html  css  js  c++  java
  • (转载)python3中使用urlopen()报错的解决方法

    在使用python3中的urllib.request模块抓取网页的时候使用一下的代码会报一个urllib.error.URLError错误

      

    1  import urllib.request
    2     response = urllib.request.urlopen('https://www.python.org')
    3 
    4 urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)>



    这个错误是因为Python 2.7.9 之后引入了一个新特性,当你使用urllib.urlopen一个 https 的时候会验证一次 SSL证书。当目标使用的是自签名的证书时就会报urllib.error.URLError错误。解决方法如下:

       

    import urllib.request
        import ssl
        ssl._create_default_https_context = ssl._create_unverified_context
        response = urllib.request.urlopen('https://www.python.org')
        print(response.read().decode('utf-8'))


    通过导入ssl模块把证书验证改成不用验证就行了。
    ————————————————
    版权声明:本文为CSDN博主「悠闲独自在」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_25403205/article/details/81258327

  • 相关阅读:
    丑数(摘)
    queue 之团队队列(摘)
    stack 集合栈计算机 (摘)
    反片语(map)
    stl的集合set——安迪的第一个字典(摘)
    stringstream函数(i o)
    T
    S
    R
    java面试题
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/13368552.html
Copyright © 2011-2022 走看看