<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Python on IT Space</title><link>https://blog.jiatool.com/en/tags/Python/</link><description>Recent content in Python on IT Space</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>jia@jiatool.com (Jia)</managingEditor><webMaster>jia@jiatool.com (Jia)</webMaster><copyright>&amp;copy;{year}, Jia All Rights Reserved</copyright><lastBuildDate>Sun, 17 Jan 2021 20:30:00 +0800</lastBuildDate><atom:link href="https://blog.jiatool.com/en/tags/Python/index.xml" rel="self" type="application/rss+xml"/><item><title>Python Requests Error: requests.exceptions.SSLError: certificate verify failed - Unable to Access Web Page</title><link>https://blog.jiatool.com/posts/requests_ssl_error/</link><pubDate>Sun, 17 Jan 2021 20:30:00 +0800</pubDate><author>jia@jiatool.com (Jia)</author><atom:modified>Sun, 17 Jan 2021 20:30:00 +0800</atom:modified><guid>https://blog.jiatool.com/posts/requests_ssl_error/</guid><description>Issue When using the Requests package to fetch a webpage, you might sometimes encounter the following error: requests.exceptions.SSLError: HTTPSConnectionPool(host='www.xxx.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),)) The cause is often related to HTTPS SSL certificate issues—such as the certificate being expired, invalid, or having other proble</description><content:encoded>&lt;h2 id="issue">Issue&lt;/h2>
&lt;p>When using the Requests package to fetch a webpage, you might sometimes encounter the following error:&lt;/p>
&lt;pre>&lt;code>requests.exceptions.SSLError: HTTPSConnectionPool(host='www.xxx.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))
&lt;/code>&lt;/pre>&lt;p>The cause is often related to HTTPS SSL certificate issues—such as the certificate being expired, invalid, or having other problems—which leads Requests to fail verification during the request and results in the requests.exceptions.SSLError error.&lt;/p>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/requests_ssl_error/invalid_certificate.png" alt="Invalid Certificate" data-caption="Invalid Certificate" src="data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' width='500px' height='' viewBox='0 0 24 24'%3E%3Cpath fill='none' d='M0 0h24v24H0V0z'/%3E%3Cpath fill='%23aaa' d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z'/%3E%3C/svg%3E" class="lazyload" style="width:500px;height:;"/>
&lt;figcaption style="text-align: center;">
Invalid Certificate
&lt;/figcaption>
&lt;/figure>
&lt;br/>
&lt;h2 id="solution">Solution&lt;/h2>
&lt;p>By default, the &lt;a href="https://requests.readthedocs.io/en/master/user/advanced/#ssl-cert-verification" target="_blank" rel="noopener">
Requests package
&lt;/a> has SSL verification enabled. One way to resolve this issue is to disable SSL certificate verification by adding the parameter &lt;code>verify=False&lt;/code> to the request.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="n">r&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">requests&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;https://www.xxx.com&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">verify&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="bp">False&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;br/>
&lt;hr />
&lt;p>Disabling SSL certificate verification in the Requests package allows you to retrieve the webpage data, but it will also display a warning:&lt;/p>
&lt;pre>&lt;code>InsecureRequestWarning: Unverified HTTPS request is being made to host 'www.xxx.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
&lt;/code>&lt;/pre>&lt;p>While this warning does not affect the result, you might find it distracting. To hide this warning message, refer to the following guide (&lt;a href="https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings">https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings&lt;/a>), which states: &amp;ldquo;if you understand the risks and wish to disable these warnings, you can use &lt;code>disable_warnings()&lt;/code>.&amp;quot;:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="kn">import&lt;/span> &lt;span class="nn">urllib3&lt;/span>
&lt;span class="n">urllib3&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">disable_warnings&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Alternatively, you can hide warnings for only unsafe requests:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-Python" data-lang="Python">&lt;span class="kn">import&lt;/span> &lt;span class="nn">urllib3&lt;/span>
&lt;span class="n">urllib3&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">disable_warnings&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">urllib3&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">exceptions&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">InsecureRequestWarning&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;br/>
&lt;hr />
&lt;p>References:&lt;br />
&lt;a href="https://requests.readthedocs.io/en/master/user/advanced/#ssl-cert-verification" target="_blank" rel="noopener">
Requests package - SSL Cert Verification
&lt;/a>&lt;br />
&lt;a href="https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings" target="_blank" rel="noopener">
urllib3 package - TLS Warnings
&lt;/a>&lt;br />
&lt;a href="https://stackoverflow.com/questions/27981545/suppress-insecurerequestwarning-unverified-https-request-is-being-made-in-pytho" target="_blank" rel="noopener">
stackoverflow
&lt;/a>&lt;/p>
&lt;br/>
&lt;blockquote>
&lt;p>真正能讓你倒下的，不是對手，而是你絕望的內心。&lt;/p>
&lt;/blockquote></content:encoded><dc:creator>Jia</dc:creator><media:content url="https://blog.jiatool.comimages/cover/requests_ssl_error.jpg" medium="image"><media:title type="html">featured image</media:title></media:content><media:content url="https://blog.jiatool.comimages/posts/requests_ssl_error_meta.jpg" medium="image"><media:title type="html">meta image</media:title></media:content><category>Python</category><category>Requests</category><category>SSL</category><category>web crawler</category></item></channel></rss>