python urllib.request ssl失败_python requests SSL证书问题
錯(cuò)誤信息:
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
python做爬蟲,對(duì)于有的網(wǎng)站,需要驗(yàn)證證書,比如:12306。
百度查報(bào)錯(cuò)信息,stack overflow里講的比較好。
cafile = 'cacert.pem' # http://curl.haxx.se /ca/cacert.pem
r = requests.get(url, verify=cafile)
requests的官方相關(guān)幫助
就是說加上一個(gè)參數(shù):verify=證書路徑,或verify=False
我測(cè)試了一下,后者會(huì)有警告,提示安全問題
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)
使用證書的最大問題是證書在哪兒弄?,經(jīng)過探索得到解決:通過瀏覽器查看網(wǎng)頁證書,然后另存為
百度經(jīng)驗(yàn)貼:下載安全證書
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author : 西瓜,2017/4/29 0:30
# @File : 12306測(cè)試.py
# python版本:python3.5
my_header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Referer': 'http://www.baidu.com',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Host':None
}
import requests
from bs4 import BeautifulSoup
geturl ="https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.\
train_date=2017-04-29&leftTicketDTO.from_station=WHN&leftTicketDTO.\
to_station=SZN&purpose_codes=ADULT"
# 測(cè)試網(wǎng)址
# geturl = "http://blog.csdn.net/wangming520liwei/article/details/53896964"
# res = requests.get(geturl,headers=my_header)
res = requests.get(geturl,headers=my_header,verify=False)
# res = requests.get(geturl,headers=my_header,verify="E:/SRCA.crt")
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html5lib')
總結(jié)
以上是生活随笔為你收集整理的python urllib.request ssl失败_python requests SSL证书问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python运行mcmc为何老出错_py
- 下一篇: patch是什么意思啊_学 Vue 看这