python 进阶知识点selenium的cookies转变成requests的cookies

轩辕乐邦
2023-12-01
一. 将selenium的cookies转换成requests的字符串cookies

1.selenium的cookies写入json文件

with open('cookies.json','w') as f:
                f.write(json.dumps(driver.get_cookies()))

2.selenium的cookies改成requests字符串cookies

self.cookies = '; '.join(item for item in [item["name"] + "=" + item["value"] for item in driver.get_cookies()])
cookies = dict(cookies_are=self.cookies)
res=requests.get(url,cookies=cookies)

二、 lambda用法

#以下写法是统计某json接口字典里面grade的个数
    def _number(slef,sorted_x,value):
        x=lambda sorted_x,value: [countx['Grade'] for countx in sorted_x].count(value)
        return x(sorted_x,value)

三、一行代码将字符串cokies转成字典

    cookies = dict([l.split("=", 1) for l in cookies.split("; ")])

 类似资料: