Best Practice to OpenerDirector Ignore SSL Verification in Python 3.x – Python Web Crawler Tutorial

By | July 21, 2019

In python 3.x, we have two methods to open a url.

First is:

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

Second is:

OpenerDirector.open(url, data=None[, timeout])

As urllib.request.urlopen, if you want to ignore ssl verification, you can read this tutorial.

Best Practice to urllib.request Ignore SSL Verification in Python 3.x – Python Web Crawler Tutorial

How about OpenerDirector.open()? 

In this tutorial, we will introduce you how to ignore ssl verification if you use OpenerDirector.open() to open a https url.

Preliminaries

import ssl
import urllib

Create a unverified context

context=ssl._create_unverified_context()

Create a HTTPSHandler object with context

sslHandler = urllib.request.HTTPSHandler(context=context)

Create a OpenerDirector object with HTTPSHandler object

 opener = urllib.request.build_opener(sslHandler)

Then you can use this opener object to open a https url with ingoring ssl virification.

Open url with opener

crawl_response = opener.open(crawl_url='https://www.facebook.com/', timeout = 30)

Leave a Reply