selenium结合requests实现session会话

requests实现session会话
import requests
session =
session = requests.session()
def cookie_to_cookiejar(cookies):
    if not hasattr(cookies, "startswith"):
        raise TypeError
    import requests
    cookiejar = requests.utils.cookiejar_from_dict(
        {cookie[0]: cookie[1] for cookie in
         [cookie.split("=", maxsplit=1) for cookie in cookies.split(";")]})
    return cookiejar
url = "https://baidu.com"
bro = webdriver.Chrome(executable_path='chromedriver')
bro.get(url=url)
bro.implicitly_wait(10)
cookies = bro.get_cookies()
cookie = ""
for dic1 in cookies:
    name = dic1.get('name')
    value = dic1.get('value')
    cookie = cookie + f"{name}={value}; "

bro.quit()
cookiejar = cookie_to_cookiejar(cookie.strip('; '))
session.cookies = cookiejar

 

posted @ 2021-01-30 13:45  凯帅  阅读(953)  评论(0编辑  收藏  举报