【selenium 获取cookie】
golang代码
//生成带有cookie的header
func buildHeader(chromeDriverPath string, url string) map[string]string { opts := []selenium.ServiceOption{ //selenium.Output(os.Stderr), // Output debug information to STDERR. } //selenium.SetDebug(true) service, err := selenium.NewChromeDriverService(chromeDriverPath, 9515, opts...) //port端口随便填,保证前后一致if err != nil { panic(err) // panic is used only as an example and is not otherwise recommended. } defer service.Stop() caps := selenium.Capabilities{"browserName": "chrome"} wd, err := selenium.NewRemote(caps, "http://127.0.0.1:9515/wd/hub") if err != nil { panic(err) } defer wd.Quit() if err := wd.Get(url); err != nil { panic(err) }// 下面这两行是点击登录的代码.不同的网址肯定不一样we, err := wd.FindElement(selenium.ByXPATH, "xxxxxx") we.Click()time.Sleep(time.Second * 20) cookies, err := wd.GetCookies() var tempList = make([]string, len(cookies)) for index, cookie := range cookies { tempList[index] = fmt.Sprintf("%v=%v", cookie.Name, cookie.Value) } cookieStr := strings.Join(tempList, "; ") header := map[string]string{} header["Cookie"] = cookieStr header["Content-Type"] = "application/json;charset=utf-8" return header
}//使用上面的header发出request请求
func request(method string, url string, reqBodyStr string) (*http.Response, error) { t := http.DefaultTransport.(*http.Transport).Clone() t.MaxIdleConnsPerHost = 2048 t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} t.MaxConnsPerHost = 20 client := &http.Client{ Timeout: time.Minute, Transport: t, } req, _ := http.NewRequest(method, url, strings.NewReader(reqBodyStr)) return client.Do(req)
}
python代码
# 生成带有cookie的header
def build_header(url, chrome_driver_path): driver = webdriver.Chrome(chrome_driver_path) driver.implicitly_wait(60) driver.maximize_window() driver.get(url) # 点击登录按钮 ,并等待页面跳转完成 ,此处随意发挥 driver.find_element_by_xpath( 'xxx').click() time.sleep(20) # 获取cookies cookies = driver.get_cookies() driver.close() temp_list = [] for cookie in cookies: temp_list.append('{}={}'.format(cookie['name'], cookie['value'])) CookieStr = '; '.join(temp_list) return { "Cookie": CookieStr }# 使用上面的header发出request请求
def request(url,header):resp = requests.get(url=url, headers=header, verify=False)return resp
【selenium 获取cookie】相关推荐
- Python用selenium获取cookie以后给rqeuests使用。
先使用selenium的get_cookies()方法获取cookie,再使用 requests.Session() 使用cookie 获取 首先用selenium自带的get_cookies()方法 ...
- python+selenium获取cookie session_Python Selenium模拟登录成功后,使用此cookie、利用requests库进行get时,提示“非法登陆”。...
一. 步骤概述 a. 模拟登录学校选课系统(使用Selenium库登陆http://xk.suibe.edu.cn/xsxk/login.xk) b. 取得cookie后传入requests的sess ...
- python+selenium获取cookie session_selenium获取cookie及设置cookie
获取cookie:在打开的页面使用 self.driver.get_cookies() 返回下面数据: [{'domain': '.linkedin.com', 'expiry': 1553, 'ht ...
- Python用selenium获取Cookie并用于登录。
不能一味地看网上的代码,Cookie代码格式有的不一样 selenium官网有自带的中文教程,但是有点不仔细.中文官方连接在这3-7 用贴吧举个例吧 上连接 http://www,baidu.com ...
- python爬虫(十四)selenium(select、17素材网、模拟登录豆瓣和QQ空间、获取cookie、行为链)
selenium介绍(下) 不管页面是动态加载还是静态加载出来的,只要是elements中能找到的,都能用selenium来获取数据,selenium获取数据的方式是以页面最终渲染后的前端为基础的,不 ...
- 获取cookie的几种方式
在爬虫中cookie是非常有用的,可以解决反爬,封号等问题.接下来我们来说说获取cookie的集中方式. 这里采用python2.7,本来我都是用python3.6的,来了公司之后,公司适用版本2.7 ...
- 爬虫中获取cookie的方式
为什么要获取cookie? 因为有的页面爬取的时候,需要登录后才能爬,比如知乎,如何判断一个页面是否已经登录,通过判断是否含有cookies就可以,我们获取到cookie后就可以携带cookie来访问 ...
- 爬虫三(Bs4搜索、Selenium基本使用、无界面浏览器、Selenium自动登录百度案例、自动获取12306登录验证码案例、切换选项卡、浏览器前进后退、登录Cnblogs获取Cookie自动点赞)
文章标题 一.Bs4搜索文档树 二.CSS选择器 三.selenium基本使用 四.无界面浏览器 五.selenium其他使用 1)自动登录百度案例 2)获取位置属性大小.文本 3)自动获取12306 ...
- python selenium根据url获取cookie信息
在python中如何用 selenium获取指定页面的cookie信息呢? 注意:本实例仅适用于Chrome浏览器,其它浏览器不支持,需要下载对应Chrome浏览器版本的浏览器驱动包. 目录 1.查看 ...
最新文章
- 某头条程序员吐槽:前同事银行流水造假,背调也是找同事编的!工资翻了300%!自己老实巴交提供真实银行流水,却惨遭压薪!...
- 使用mysql_MySQL 的安装与使用(一)
- 牛客国庆集训派对Day2 F、平衡二叉树 【构造+记忆化搜索】
- 【视频课】超全目标检测课程!超30小时理论与4大案例实践,检测框架使用,长期更新中...
- 《快速构建Windows 8风格应用》系列文章汇总
- MongoDB学习2——Windows 使用mongo连接数据库
- Xcode9的xib只支持iOS7.0及以上版本
- openwrt 3g模块上网
- ParaFi Capital资产管理规模超1亿美元,至少投资22家公司或协议
- 查询oracle 数据库中回滚段中一个时间点被修改的表数据并还原表中原来数据
- windows获取cmd回显以及获取路径
- 初识TensorFlow
- SVM(三)—Kernels(核函数)
- ios plist获取权限
- 5.2刚买了佳能的ixus100is数码相机,可以录像不过是mov格式的,需要转换为dvd的软件...
- u盘安装linux6.5教程,u盘安装CentOS6.5
- CSDN自定义模块简单设置之——添加图片、文字、链接等
- 外设驱动库开发笔记28:W5500以太网控制器
- JAVAWEB-NOTE03
- 【云速建站】域名配置指导