我复制cookie 希望通过算法自动分解成字典格式传入
self.cookie_str="acw_tc=b65cfd2515395760831792797e7a30fed7278a95d7c68d0dcad0b9cbc4ac1b; kwlo_iv=1h; kr_stat_uuid=CQ5e225664238; Hm_lvt_e8ec47088ed7458ec32cde3617b23ee3=1539833640,1539835270,1539847843,1539854305; Hm_lpvt_e8ec47088ed7458ec32cde3617b23ee3=1539854305; download_animation=1; _kr_p_se=3bc105b4-9a7d-42fb-add8-bf95717e809e; krid_user_id=1174461189; krid_user_version=4; kr_plus_id=1174461189; kr_plus_token=sxXE3YtIANlixxyKQlgRsxUW9bw5_1182767____; device-uid=010004b0-d346-11e8-a96a-4199591fdd87; Hm_lvt_713123c60a0e86982326bae1a51083e1=1539836728,1539847818,1539915819,1540172654; Hm_lvt_1684191ccae0314c6254306a8333d090=1539836728,1539847818,1539915819,1540172655; kr_plus_utype=0; krnewsfrontss=4963ea68949936658d715f1da01ac2dc; M-XSRF-TOKEN=82688f4b00348470d9150fa6723dd91d4ef3037d93ccf1c010c11e523f0c4d80; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221174461189%22%2C%22%24device_id%22%3A%221668a22a82493-063c5cb7bcafff-163b6953-1296000-1668a22a82533c%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%221668a22a82493-063c5cb7bcafff-163b6953-1296000-1668a22a82533c%22%7D; Hm_lpvt_713123c60a0e86982326bae1a51083e1=1540189058; Hm_lpvt_1684191ccae0314c6254306a8333d090=1540189059; Z-XSRF-TOKEN=eyJpdiI6IkNoUVM5ZUxnSTZlUkhTQlNkMmtzdXc9PSIsInZhbHVlIjoiUG11V2xcLzZXZElMWjRBY3l1MWxvc3ZFOWQ4S1pRNXd2b0FJSUk3Y01ZRHJSV1d3OEd3N25zcUNRaWFRYVpvenI0WmNlZmt4VStrU0NMYnc3XC94cklzUT09IiwibWFjIjoiYWY2OTFlNDA4NzNhYjAwYjg4MmZlYWZmNTljNmY4ZjY3ZTFhNjkwNzcxM2Y0MmQ5MDBlOTg5NzMwYjJlMjE3YiJ9; krchoasss=eyJpdiI6IjZJNXE4T0lkVXhMdjAxXC9OeDh3OUp3PT0iLCJ2YWx1ZSI6IjBQUm1cL1lQS2RpUFRmdndLN3ExYkdXdXVjSE9HY1RFYVB0NEh0S3VwbHhLMUczQlJ3RHdmMXVldVRZZ0JUeGVSWjY2a1R1QjZkMWd1RFdja2RBaTU1QT09IiwibWFjIjoiZWQzMmJmNDNkZTYzMTIwZTQwZGU1ZDdkM2EyN2Q2YWRiMzg2YmQ4ZGIyZmE4MjI1YjgxZGI0YzY2YzMzMzAxMSJ9"
def get_cookies(cookies_str):
cookies = {}
for line in cookies_str.split(';'):
name, value = line.strip().split('=', 1)
cookies[name] = value
return cookies
传入str 然后分割,但是最近这个cookie_str无法分割,求指教,谢谢!
1 回答
胡子哥哥
TA贡献1825条经验 获得超6个赞
先说说你的代码:
- return cookies 不能写在 for 循环里面。(这是错误的根本原因)
- 在 cookie 的键和值里面一般不会出现空格的,所以,你可以放心的
cookies_str.split("; ")
,这样下面那行就不需要使用 strip() 了
使用更 pythonic 的方法:
return dict(map(lambda x: x.split("=", 1), cookie_str.split("; ")))
添加回答
举报
0/150
提交
取消