- 錯誤信息:
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
python做爬蟲验庙,對于有的網(wǎng)站仿村,需要驗證證書,比如:12306。
百度查報錯信息,stack overflow里講的比較好。
cafile = 'cacert.pem' # http://curl.haxx.se /ca/cacert.pem
r = requests.get(url, verify=cafile)
requests的官方相關(guān)幫助
就是說加上一個參數(shù):verify=證書路徑,或verify=False
我測試了一下,后者會有警告才沧,提示安全問題
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)驗貼:下載安全證書
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author : 西瓜,2017/4/29 0:30
# @File : 12306測試.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"
# 測試網(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')