关闭requests请求的长连接


import requests,json,time
requests.packages.urllib3.disable_warnings()
requests.adapters.DEFAULT_RETRIES = 5   #增加重连次数

def get_price(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36',
        'Referer': 'https://www.baidu.com',
    }
    s = requests.session()
    s.keep_alive = False  # 关闭多余连接
    response = s.get(url=url,headers=headers,verify=False)
    text_0 = response.text[14:-1]
    try:
        text = json.loads(text_0,strict=False)
    except Exception as e:
        print(e)
        print(text_0)
        print(text)
        return
    # print(text)
    return text

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM