python用selenium打开chrome浏览器保持登录方式
作者:修炼之路 时间:2022-02-19 10:28:08
导读
我们在使用selenium
打开google浏览器的时候,默认打开的是一个新的浏览器窗口,而且里面不带有任何的浏览器缓存信息。当我们想要爬取某个网站信息或者做某些操作的时候就需要自己再去模拟登陆
selenium操作浏览器
这里我们就以CSDN为例,来展示如何让selenium在打开chrome浏览器的时候带上用户的登录信息
打开chrome浏览器
from selenium import webdriver
from selenium.webdriver import ChromeOptions
#设置操作的网站
web_url = "https://bbs.csdn.net"
browser = webdriver.Chrome(executable_path=r"D:\chromedriver_win32\chromedriver\chromedriver.exe")
#打开网页
browser.get(web_url)
运行程序之后,打开浏览器的界面如上图所示,可以看出来默认是没有带用户的登录信息的
带用户登录信息打开chrome浏览器 打开带有用户信息的chrome窗口
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -remote-debugging-port=9014 --user-data-dir="C:\\Users\\15053\AppData\Local\Google\Chrome\\User Data"
因为安装chrome的时候是采用的默认安装路径,所以路径就和上面一样。如果安装的时候自定义了路径,就注意修改一下chrome.exe
的路径。
user-data
目录是chrome
缓存数据的目录,里面包含了用户的登录信息。如果你是在你自己的电脑上使用,需要将15053
修改成你自己的用户名。
注意:在执行上面命令的时候建议关闭chrome浏览器
,否则后面在执行python程序的时候,可能无法连接到chrome。
使用selenium打开网站
from selenium import webdriver
from selenium.webdriver import ChromeOptions
web_url = "https://bbs.csdn.net"
#加载cookies中已经保存的账号和密码
options = ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9014")
browser = webdriver.Chrome(executable_path=r"D:\chromedriver_win32\chromedriver\chromedriver.exe",
chrome_options=options)
browser.get(web_url)
可以看到,此时打开的网站已经自带了用户的登录信息
来源:https://blog.csdn.net/sinat_29957455/article/details/122242196
标签:python,selenium
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python之如何查找多层嵌套字典的值
2021-12-05 08:57:07
Python中使用中文的方法
2021-09-26 05:46:39
Linux安装Python虚拟环境virtualenv的方法
2022-07-07 00:33:36
Python数据可视化图实现过程详解
2022-08-30 19:24:17
![](https://img.aspxhome.com/file/2023/7/81557_0s.jpg)
Python PIL库图片灰化处理
2022-08-18 20:57:30
![](https://img.aspxhome.com/file/2023/7/64617_0s.png)
Linux下Python安装完成后使用pip命令的详细教程
2021-07-07 02:45:51
![](https://img.aspxhome.com/file/2023/6/72926_0s.png)
在Python的Flask框架中使用模版的入门教程
2021-09-14 04:57:59
Python高级特性——详解多维数组切片(Slice)
2023-01-20 17:04:13
Python 实现Mac 屏幕截图详解
2021-06-16 13:43:52
![](https://img.aspxhome.com/file/2023/8/107578_0s.jpg)
如何使用Index Server建立一个网站导航地图?
2010-06-05 12:39:00
Go项目实现优雅关机与平滑重启功能
2023-07-16 07:36:13
对pandas的行列名更改与数据选择详解
2022-06-09 15:46:37
![](https://img.aspxhome.com/file/2023/0/90510_0s.jpg)
网页对联广告代码效果大全
2007-10-25 23:16:00
详解Python的Django框架中的templates设置
2021-12-15 15:16:06
服务端XMLHTTP(ServerXMLHTTP in ASP)进阶应用-User Agent伪装
2008-11-11 12:29:00
![](https://img.aspxhome.com/file/UploadPic/200811/11/if8l_googlerss-96s.png)
python算法表示概念扫盲教程
2022-06-22 00:43:34
![](https://img.aspxhome.com/file/2023/9/133269_0s.jpg)
客齐集社区头像显示效果代码
2008-04-03 13:15:00
![](https://img.aspxhome.com/file/UploadPic/20084/3/200843132357900s.gif)
python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法实例
2021-07-28 18:28:45
![](https://img.aspxhome.com/file/2023/6/99446_0s.png)
PHP格式化显示时间date()函数代码
2023-06-10 17:55:01
![](https://img.aspxhome.com/file/2023/0/55460_0s.png)
pandas修改DataFrame列名的方法
2021-07-18 16:41:24