利用Cookie免密登录
利用cookie完成网站的免密登录的思路:
第一阶段:
打开网站登录页面
获取登录之前的cookie
等待60s,这个时间手动去输入用户名、密码、验证码登录
获取登录之后的cookie
把登录之后的cookie与登录之前的cookie打印并进行对比
找出登录之后不同的cookie或增加cookie信息
第二阶段:
新编一个脚本
打开网站登录页面
用add_cookie()方式去逐项增加登录之后不同的cookie
刷新网站,即可完成自动登录操作,但是这是最笨的方法,我们下面可以优化脚本,继续往下看
1 import time 2 import xlrd 3 from selenium import webdriver 4 6 driver = webdriver.Firefox() 7 driver.get('https://www.cnblogs.com') 8 time.sleep(60)#等待60s,这个时间手动去输入用户名、密码、验证码登录 9 cookies = driver.get_cookies()#获取所有登录之后的cookies 10 for cookie in cookies: 11 print(cookie) 13 #获取登录后的cookies打印出来,再逐项add_cookie 14 driver.add_cookie({'name': '_gat_gtag_UA_476124_1', 'value': '1', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1662691960, 'sameSite': 'None'}) 15 driver.add_cookie({'name': '__gads', 'value': 'ID=0149c3cb735767dc-22a09fe559d600ba:T=1662691901:S=ALNI_MbF-pW1fbSM0bRsLkBF1ysOatA2VA', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1696387901, 'sameSite': 'None'}) 16 driver.add_cookie({'name': '__gpi', 'value': 'UID=0000099dd6ff9106:T=1662691901:RT=1662691901:S=ALNI_MZSKS8IPH8wxmS-oSAMSAXAlT6h5g', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1696387901, 'sameSite': 'None'}) 17 driver.add_cookie({'name': '.Cnblogs.AspNetCore.Cookies', 'value': 'CfDJ8EOBBtWq0dNFoDS-ZHPSe51Ck0ZoI8NrfGsOwWkh--bZfMgkMwTchpt05aboMAMLzA8wtZvMLTPW55oT1e9soLBCvxdHVf5AGgcQGEYitNdm3qitzZlRIlLIEZBc_jHkOGMz-yCk9x45mxZ9dOCUVaIeK7WQkXBXLy14BVXogruYQbIn9Z-XbTaPLumUi0DwHd4Nr0LF_hIqNxLnekjr7AseFOhIe33H7SnYBNPvZcLoDPEKjj-oUp_ZOBMy3YgfIYXLctZctcXi70lYK5ulE83En3w3Ut-s4oiD2FWTfXzG8ohgCyxzqfdbgtJ-WZwH8lMZ4ShLirCzPy3uTicj6yC5nAXu-nA3yQBE--VHCGnX85dfqGW0kkZbt2DG9D1x01QxgdwPs-PQoezO3VUknFkONqb1MPxJQ-3EukgBoqlyiI4m3w25e2O1YuGa7hMt-O2WlTTroVaAMsRTS_t21l0RQ52g4b1qBO4pmgAgjXo3XQfX9MPTJmbkxGfyZhwuUfLXN5G2BLAev39WwVsZSVFGYMlbYoiL7r-lcJ6UXOdN_ZtBmdWv1TSqS_p9nqyGdQ', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': True, 'expiry': 1663987948, 'sameSite': 'None'}) 18 driver.add_cookie({'name': '.CNBlogsCookie', 'value': '10C0384E51E4D928EFCFEA8247A0E2762CAC83478CBCFAC644911D29A9D8670544AF6F1EE0DC58BFFE1032774E90B3843E7F648976BDFE212B44E6628F09DA56E1DFB185B2F2E0041701CEB3B73EC32A24BE32BB', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': True, 'expiry': 1663987948, 'sameSite': 'None'}) 19 driver.add_cookie({'name': '_ga_3Q0DVSGN10', 'value': 'GS1.1.1662691903.1.1.1662691947.16.0.0', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1725763947, 'sameSite': 'None'}) 20 driver.add_cookie({'name': '_ga', 'value': 'GA1.2.669334368.1662691900', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1725763948, 'sameSite': 'None'}) 21 driver.add_cookie({'name': '_gid', 'value': 'GA1.2.1402306673.1662691900', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1662778348, 'sameSite': 'None'}) 22 driver.add_cookie({'name': 'Hm_lvt_866c9be12d4a814454792b1fd0fed295', 'value': '1662691900', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'expiry': 1694227948, 'sameSite': 'None'}) 23 driver.add_cookie({'name': 'Hm_lpvt_866c9be12d4a814454792b1fd0fed295', 'value': '1662691948', 'path': '/', 'domain': '.cnblogs.com', 'secure': False, 'httpOnly': False, 'sameSite': 'None'}) 24 driver.add_cookie({'name': '.AspNetCore.Antiforgery.b8-pDmTq1XM', 'value': 'CfDJ8EOBBtWq0dNFoDS-ZHPSe51yp-aqgjItNaxXRuOeMKYd6EuiDOiPQ46z-RS-pAkwp5Jtd6e87uGVGgACLtdDht6lXohnlkk3INO0VF84uM5QjnNlT9SNhFtma2KsHIDH3PxJDiTUg_oOf4ZqQ_yFheM', 'path': '/', 'domain': 'www.cnblogs.com', 'secure': False, 'httpOnly': True, 'sameSite': 'None'}) 25 time.sleep(2) 26 driver.refresh()
第三阶段:优化脚本,利用第三方模块xlwt先将获取的cookies保存到excel中
先编写一个get_cnblogs_cooke.py脚本获取cookie并保存到blogs_cookies.xls文件中
安装命令:pip3 install xlwt
1 import time 2 import xlwt 3 from selenium import webdriver 4 5 driver = webdriver.Firefox() 6 driver.get('https://www.cnblogs.com') 7 time.sleep(30) 8 cookies = driver.get_cookies() 9 10 workbook = xlwt.Workbook(encoding='utf-8') # 创建一个workbook并设置编码 11 worksheet = workbook.add_sheet('sheet1') # 添加sheet 12 worksheet.write(0,0,'name') #第一行第一列写入内容 13 worksheet.write(0,1,'value') 14 worksheet.write(0,2,'path') 15 worksheet.write(0,3,'domain') 16 worksheet.write(0,4,'secure') 17 worksheet.write(0,5,'httpOnly') 18 worksheet.write(0,6,'sameSite') 19 for i in range(1,len(cookies)+1): #1,2,3,4,5,6 20 worksheet.write(i,0,cookies[i-1]['name']) 21 worksheet.write(i,1,cookies[i-1]['value']) 22 worksheet.write(i,2,cookies[i-1]['path']) 23 worksheet.write(i,3,cookies[i-1]['domain']) 24 worksheet.write(i,4,cookies[i-1]['secure']) 25 worksheet.write(i,5,cookies[i-1]['httpOnly']) 26 worksheet.write(i,6,cookies[i-1]['sameSite']) 27 workbook.save('blogs_cookies.xls') # 将以上内容保存到指定的文件中
第四阶段:优化脚本,利用第三方模块xlrd读取刚刚已经保存到excel的cookies
在编写一个脚本cnblogs_login_by_cookie.py读取excel并登录
安装命令:pip3 install xlrd
1 import time 2 import xlrd 3 from selenium import webdriver 4 5 driver = webdriver.Firefox() 6 driver.get('https://www.cnblogs.com') 7 workbook_open = xlrd.open_workbook('blogs_cookies.xls')#使用xlrd创建一个工作薄对象 8 sheet = workbook_open.sheet_by_name('sheet1') #根据工作表的名称创建表格对象 9 cookie_list = [] #创建一个空列表 10 for row_num in range(1,sheet.nrows): 11 cookie_dict = {} #创建一个空字典 12 cookie_dict['name'] = sheet.cell_value(row_num,0) 13 cookie_dict['value'] = sheet.cell_value(row_num,1) 14 cookie_dict['path'] = sheet.cell_value(row_num,2) 15 cookie_dict['domain'] = sheet.cell_value(row_num,3) 16 cookie_dict['secure'] = bool(sheet.cell_value(row_num,4)) #注意因为这里的值是TRUE、FALSE是布尔类型,需要强转一下,否则会当成字符串 17 cookie_dict['httpOnly'] = bool(sheet.cell_value(row_num,5)) 18 cookie_dict['sameSite'] = sheet.cell_value(row_num,6) 19 cookie_list.append(cookie_dict) #以字典形式装入列表,[{'name':'','value':'','path':''},{},{},{},{},{}] 20 for cookie in cookie_list: 21 driver.add_cookie(cookie) #再用for循环输出并add_cookie 22 time.sleep(3) 23 driver.refresh() #刷新浏览器即可免密登录