1 # -*- coding: utf-8 -*- 2 """ 3 Created on Mon Dec 12 14:35:49 2016 4 5 @author: yaru 6 """ 7 8 targetURL = "http://pythonscraping.com" 9 #targetURL = "https://github.com/" 10 11 from selenium import webdriver 12 driver = webdriver.PhantomJS(executable_path=r"D:\Anaconda2\Scripts\phantomjs-2.1.1-windows\bin\phantomjs.exe") 13 driver.get(targetURL) 14 driver.implicitly_wait(1) 15 #print(driver.get_cookies()) 16 17 savedCookies = driver.get_cookies() 18 driver2 = webdriver.PhantomJS(executable_path=r"D:\Anaconda2\Scripts\phantomjs-2.1.1-windows\bin\phantomjs.exe") 19 driver2.get(targetURL) 20 driver2.implicitly_wait(1) 21 driver2.delete_all_cookies() 22 23 for cookie in savedCookies: 24 # fix the 2nd problem 25 for k in ('name', 'value', 'domain', 'path', 'expiry'): 26 if k not in list(cookie.keys()): 27 if k == 'expiry': 28 cookie[k] = 1475825481 29 # fix the 1st problem 30 driver2.add_cookie({k: cookie[k] for k in ('name', 'value', 'domain', 'path', 'expiry') if k in cookie}) 31 print(cookie) 32 print("aaaa3") 33 34 driver2.get(targetURL) 35 driver2.implicitly_wait(1)
浙公网安备 33010602011771号