美文网首页
Python爬虫CookieJar与字典Dict与字符串Str的

Python爬虫CookieJar与字典Dict与字符串Str的

作者: 巨数 | 来源:发表于2020-02-04 20:12 被阅读0次

    Python爬虫CookieJar与字典Dict与字符串Str的相互转换

    导入相关包

    from requests import session, utils
    
    cookies_str ='__yadk_uid=kAOoSnKIPVG75fDchKQfie4AZOUEguU; web_login_version=MTU3OTUyODIyMQ%3D%3D--051c39349f8db5ce6deebd95f87edaf333b22c; remember_user_token=W1s3NjE0MTg2XSiJDJhxJGdFTmdvOVR1UnFEZ25MOVdidk1tZU8iLCIxNTgwMzU3ODYwLjcwOTAxMDQiXQ%3D%3D--987f10b49acd364076a7c62164e864c9b74543f; _m7e_session_core=c1047556f8976a3158409ae2cecb6c4; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%226f7642c8fd9b-0154843ff21c89-6701b35-1327104-16f7642c8fe840%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%8%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%2%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22%24device_id%22%3A%22167642c8fd9b-0154843ff21c89-6701b35-1327104-16f764c8fe840%22%7D; read_mode=day; default_font=font2; locale=zh-CN; Hm_lvt_0c0e9d9b1e7d6173e6842e85b9fb068=1580360169,158066889,1580376539,1580386305; Hm_lpvt_0c0e9d9b1e7d61b3e6842e85bfb068=1580386305'
    # cookie字符串
    
    cookies_dic = dict([co.strip().split('=') for co in cookies_str.split(';')])
    # 字典
    

    转化为CookieJar格式

    s =session()
    cookies_jar = utils.cookiejar_from_dict(cookies_dic)
    s.cookies = cookies_jar
    print(s.cookies)
    

    相关文章

      网友评论

          本文标题:Python爬虫CookieJar与字典Dict与字符串Str的

          本文链接:https://www.haomeiwen.com/subject/zoedxhtx.html