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