python3 小實踐(一)——selenium獲取的cookie傳遞
阿新 • • 發佈:2019-02-15
from selenium import webdriver from time import sleep import requests import pickle #獲取登入後的cookies class get_cookie1: def __init__(self, url): self.url = url def get_cookie2(self): driver = webdriver.Chrome() r = driver.get(self.url) driver.find_element_by_xpath("//input[@name='userName']").send_keys('admin') driver.find_element_by_xpath("//input[@name='password']").send_keys('123456') driver.find_element_by_xpath("//input[@id='btnlogin']").click() sleep(10) list_cookies= driver.get_cookies() print(list_cookies) cookie={} # 轉換dict呼叫 for item in list_cookies: cookie[item['name']]=item['value'] print(cookie) cookie= driver.get_cookies() #print(cookie) self.cookie = cookie #print(self.cookie) return self.cookie if__name__ =='__main__': c = get_cookie1("url") c.get_cookie2()
通過selenium提取的cookie轉換一下格式就可以直接呼叫
python新人,程式碼寫的有點挫,給自己獲取的新知識點標個記