requests库验证SSL的问题

今天在用requests爬取网站数据时,遇到了一个问题,代码和报错信息如下:

import re

import requests
class Handle_Lagou(object):
    def __init__(self):
        #使用session保存cookies信息
        self.lagou_session = requests.session()
        self.header = {
            'Connection': 'close',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
        }
        self.city_list=""
    #获取全国所有城市列表的方法
    def handle_city(self):
        city_search = re.compile(r'zhaopin/">(.*?)</a>')
        city_url = "https://www.lagou.com/jobs/allCity.html"
        city_result = self.handle_request(method="GET",url=city_url)
        print(city_result)

    def handle_request(self,method,url,data=None,info=None):
        if method == "GET":
            response = self.lagou_session.get(url=url,headers=self.header)
        return  response.text

if __name__=='__main__':
    lagou = Handle_Lagou()
    lagou.handle_city()

运行后发现出现了两个错误

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.lagou.com', port=443): Max retries exceeded with url: /jobs/allCity.html (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))

requests.exceptions.SSLError: HTTPSConnectionPool(host='www.lagou.com', port=443): Max retries exceeded with url: /jobs/allCity.html (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))

然后就上网找了找,看了大部分相关问题的博客后,找到了两个解决办法(其实是一个):

import re

import requests
#在此处设置取消警告信息
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 这是另一个大佬的办法,可惜过于复杂,我没看懂,简简单单才是真 
# requests.packages.urllib3.disable_warnings()
# requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
# try:
#     requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
# except AttributeError:
#     # no pyopenssl support used / needed / available
#     pass

class Handle_Lagou(object):
    def __init__(self):
        #使用session保存cookies信息
        self.lagou_session = requests.session()
        self.header = {
            'Connection': 'close',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
        }
        self.city_list=""
    #获取全国所有城市列表的方法
    def handle_city(self):
        city_search = re.compile(r'zhaopin/">(.*?)</a>')
        city_url = "https://www.lagou.com/jobs/allCity.html"
        city_result = self.handle_request(method="GET",url=city_url)
        print(city_result)

    def handle_request(self,method,url,data=None,info=None):
        if method == "GET":
            # 在此处设置verify = False 
            response = self.lagou_session.get(url=url,headers=self.header,verify=False)
        return  response.text

if __name__=='__main__':
    lagou = Handle_Lagou()
    lagou.handle_city()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 声明:本文讲解的实战内容,均仅用于学习交流,请勿用于任何商业用途! 一、前言 强烈建议:请在电脑的陪同下,阅读本文...
    Bruce_Szh阅读 12,842评论 6 28
  • jupyter 是一个简易的,方便的写Python代码的工具包,requests是Python里非常好用的用来发送...
    终可见丶阅读 25,568评论 10 43
  • 虞美人·夏旱 YBY 汉城湖的诗韵 作 烧火赤阳何时了? 枯叶落多少。 孤寞山村又山风, 凄凄黄草折腰荒山中。 忆...
    ce18140bccc1阅读 219评论 0 0
  • 姓名 黄中明 公司 448期学员 472期志工 522期志工 534期志工 17期服务志工 【日精进打卡...
    黄中明阅读 338评论 0 1
  • 过了下午3点,明晃晃的超市里不在熙熙攘攘,匆忙吃过午饭后我就一直站在试吃摊边推销一款新口味的饼干,现在全身僵硬。我...
    小呆瓜阅读 155评论 0 1