如何使用 urllib 修复 SSL 证书错误? [英] How to fix a SSL Certificate error with urllib?

查看:38
本文介绍了如何使用 urllib 修复 SSL 证书错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 python 程序来从网站上抓取所有图像并将它们下载到一个文件夹中,同时创建一个 csv 文件来存储所有这些信息.我正在使用 urllib 并继续收到有关 ssl 证书失败的错误.我在 Jupyter Notebook、Windows 10 和 Python 3.7 上运行.

I'm developing a python program to grab all images from a website and download them to a folder along with creating a csv file to store all of this information. I'm utilizing urllib and continue to get an error about ssl certificate failure. I'm running on Jupyter notebook, Windows 10, and Python 3.7.

我尝试通过 pip 安装 certifi 和 urllib,但这些已经很满意了.我试过重新启动 Jupyter 并不能解决问题.我不确定从哪里开始解决这个问题,因为我对 urllib 不是很熟悉.

I tried pip installing certifi and urllib but those are already satisfied. I've tried restarting Jupyter and that does not fix the problem. I'm not really sure where to start to fix this as I'm not super familiar with urllib.

我希望这会下载图像并输出到 csv 文件,并且它确实输出到 csv 文件,但是当我收到此错误时图像不会下载:

I expect this to download the images and output to the csv file, and it does output to the csv file, but the image won't download when I get this error:

该错误不会停止程序,但会抑制程序的预期功能.

The error doesn't halt the program but it does inhibit the intended function of the program.

推荐答案

如果您正在使用 urllib 库,请在请求打开 URL 时使用上下文参数.这是实现:

If you are using urllib library use context parameter when you gave request to open URL. Here is implementation:

import urllib.request

import ssl

#Ignore SSL certificate errors

ssl_context = ssl.create_default_context()

ssl_context.check_hostname = False

ssl_context.verify_mode = ssl.CERT_NONE

html = urllib.request.urlopen(url, context=ssl_context).read()

这篇关于如何使用 urllib 修复 SSL 证书错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆