Python Crawler5:设置cookie

Python Crawler5:设置cookie

设置Cookie

''' Description: henggao_note version: v1.0.0 Date: 2022-04-04 15:42:19 LastEditors: henggao LastEditTime: 2022-04-06 10:53:07 ''' from selenium import webdriver # 加载谷歌驱动 driver = webdriver.Chrome() driver.set_page_load_timeout(5000) # 防止页面加载个没完 url = "https://www.webofscience.com/" # 打开url driver.get(url) # 首先清除由于浏览器打开已有的cookies driver.delete_all_cookies() dictCookies = driver.get_cookies() for cookie in dictCookies: print(cookie.get('domain')) cookie_dict = { 'domain': cookie.get('domain'), 'expiry': cookie.get('expiry'), # 'expiry': None, 'httpOnly': cookie.get('httpOnly'), 'name': cookie.get('name'), 'path': cookie.get('path'), "secure": cookie.get('secure'), 'value': cookie.get('value'), } # 先删掉之前那次访问时的同名cookie,不然自己设置的cookie会失效 driver.delete_cookie(cookie.get('domain')) # 添加自己的cookie driver.add_cookie(cookie_dict) # driver.refresh() try: driver.get(url) # 再次打开爬取页面 print(driver.get_cookies()) # 打印设置成功的cookie except: print("ERROR")