<?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>SSL on IT 空間</title><link>https://blog.jiatool.com/tags/SSL/</link><description>Recent content in SSL on IT 空間</description><generator>Hugo -- gohugo.io</generator><language>zh</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/tags/SSL/index.xml" rel="self" type="application/rss+xml"/><item><title>Requests 套件發生 requests.exceptions.SSLError: certificate verify failed 錯誤，無法請求網頁</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>問題 在使用 Requests 套件送出請求抓取網頁時，有時候可能會遇到以下錯誤： 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)'),)) 原因是 HTTPS 的 SSL 憑證過期、不合格或有其他問題，</description><content:encoded>&lt;h2 id="問題">問題&lt;/h2>
&lt;p>在使用 Requests 套件送出請求抓取網頁時，有時候可能會遇到以下錯誤：&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>原因是 HTTPS 的 SSL 憑證過期、不合格或有其他問題，造成 Requests 在請求時證書驗證失敗，就會出現 requests.exceptions.SSLError 這樣的錯誤。&lt;/p>
&lt;figure >
&lt;img data-src="https://res.cloudinary.com/jiablog/requests_ssl_error/invalid_certificate.png" alt="憑證無效" data-caption="憑證無效" 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;">
憑證無效
&lt;/figcaption>
&lt;/figure>
&lt;h2 id="解決辦法">解決辦法&lt;/h2>
&lt;p>&lt;a href="https://requests.readthedocs.io/en/master/user/advanced/#ssl-cert-verification" target="_blank" rel="noopener">
Requests 套件
&lt;/a>在默認的情況下，預設是啟用 SSL 驗證，其中一個解決的方式是透過加上 &lt;code>verify=False&lt;/code> 參數來關閉 Requests 的 SSL 憑證檢查功能。&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>關閉 Requests 套件的 SSL 憑證檢查功能，就能正常取得網頁資料，但會跳出以下警告：&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>雖然不影響結果，但如果覺得很礙眼想隱藏此警告消息，&lt;br />
參考它給的網址(&lt;a href="https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings" target="_blank" rel="noopener">
https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
&lt;/a>)，裡頭寫到&amp;quot;如果您了解風險並希望禁用這些警告，則可以使用 &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>或者也可以只針對不安全的請求警告隱藏：&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;br/>
&lt;hr />
&lt;p>參考：&lt;br />
&lt;a href="https://requests.readthedocs.io/en/master/user/advanced/#ssl-cert-verification" target="_blank" rel="noopener">
Requests 套件 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 套件 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>網路爬蟲</category></item></channel></rss>