python3 小实践(一)——selenium获取的cookie传递

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新人,代码写的有点挫,给自己获取的新知识点标个记

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值