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

  • 相关阅读:
    CheckBox单选功能
    DOTNET
    常用命令行
    不能调试的问题的解决
    url字符串中含有中文的处理
    案例:星移eWorkflow.net系统
    使用正则表达式求完整路径中的文件名
    缺少一个***.resource的报告的解决
    Mapx中的图元移动
    Distance计算的距离随经纬度不同
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/13368552.html
Copyright © 2011-2022 走看看