python爬虫访问https网站报错解决方案ERROR:ssl_client_socket_impl.cc(1098)] handshake failed

本文介绍了一种解决Chrome浏览器中出现的SSL证书错误的方法,通过使用Selenium和添加忽略证书错误的参数来访问https://python.org网站。

报错信息:

[3488:1356:0512/211222.342:ERROR:ssl_client_socket_impl.cc(1098)] handshake failed; returned -1, SSL error code 1, net_error -101

Chrome浏览器解决方案:

from selenium import webdriver
if __name__ == '__main__':
    options=webdriver.ChromeOptions()

    options.add_argument('--ignore-certificate-errors')

    driver=webdriver.Chrome(chrome_options=options)

    driver.get(u'https://python.org/')

    driver.close()

在不同的编程语言和场景中,忽略 SSL 握手失败错误的方法有所不同,以下是几种常见语言的示例: ### PythonPython 中,使用 `requests` 库时可以通过设置 `verify=False` 来忽略 SSL 验证。 ```python import requests url = 'https://example.com' try: response = requests.get(url, verify=False) print(response.text) except requests.exceptions.RequestException as e: print(f"请求出错: {e}") ``` 不过需要注意的是,这种方式会有安全风险,因为它会接受任何 SSL 证书,可能会导致中间人攻击。为了消除警告,可以使用 `urllib3` 库来禁用警告: ```python import requests import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) url = 'https://example.com' try: response = requests.get(url, verify=False) print(response.text) except requests.exceptions.RequestException as e: print(f"请求出错: {e}") ``` ### Java 在 Java 中,可以通过自定义 `TrustManager` 来忽略 SSL 验证。 ```java import javax.net.ssl.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class IgnoreSSLVerification { public static void main(String[] args) throws Exception { // 创建一个信任所有证书的 TrustManager TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { } } }; // 安装信任管理器 SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // 创建一个不验证主机名的主机名验证器 HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; // 安装主机名验证器 HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); // 发送请求 URL url = new URL("https://example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); System.out.println(response.toString()); } } ``` ### Node.js 在 Node.js 中,使用 `https` 模块时可以通过设置 `rejectUnauthorized: false` 来忽略 SSL 验证。 ```javascript const https = require('https'); const options = { hostname: 'example.com', port: 443, path: '/', method: 'GET', rejectUnauthorized: false }; const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { console.log(data); }); }); req.on('error', (error) => { console.error(error); }); req.end(); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值