python selenium在打开的浏览器中动态调整User Agent
作者:呆萌的代Ma 时间:2022-09-26 13:41:59
核心代码是:
browser.execute_cdp_cmd('Network.setUserAgentOverride', {
"userAgent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36",
"platform": "Windows"})
使用示例:
import time
from selenium import webdriver
if __name__ == '__main__':
option = webdriver.ChromeOptions()
option.add_argument('lang=zh_CN.UTF-8') # 设置
browser = webdriver.Chrome(chrome_options=option)
browser.get("https://www.baidu.com")
print(browser.execute_script("return navigator.userAgent")) # 打印当前的userAgent
# 动态调整useragent
time.sleep(3)
browser.execute_cdp_cmd('Network.setUserAgentOverride', {
"userAgent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36",
"platform": "Windows"})
print(browser.execute_script("return navigator.userAgent"))
browser.get("https://www.baidu.com")
打印结果:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36
第一行是我的chrome
原始的header,第二行是修改后的header
并且可以在浏览器的header里看到:
来源:https://blog.csdn.net/weixin_35757704/article/details/122747126
标签:python,selenium,调整,User,Agent
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用python爬取m3u8格式视频的具体实现
2021-04-10 21:45:09
![](https://img.aspxhome.com/file/2023/7/103177_0s.jpg)
十分钟教会你用Python处理CSV文件
2022-02-05 02:24:12
![](https://img.aspxhome.com/file/2023/3/121633_0s.png)
python局域网ip扫描示例分享
2022-10-19 08:43:45
Python算法应用实战之栈详解
2022-08-22 21:56:10
![](https://img.aspxhome.com/file/2023/7/69547_0s.png)
Python 通过爬虫实现GitHub网页的模拟登录的示例代码
2022-04-27 00:26:39
![](https://img.aspxhome.com/file/2023/4/86564_0s.jpg)
Python数据类型详解(三)元祖:tuple
2021-05-17 07:49:14
Python实现统计图像连通域的示例详解
2022-12-05 09:13:05
python 三种方法提取pdf中的图片
2023-09-18 08:25:58
![](https://img.aspxhome.com/file/2023/1/95281_0s.jpg)
python回调函数中使用多线程的方法
2022-11-08 20:01:15
基于Opencv图像识别实现答题卡识别示例详解
2023-05-18 20:32:50
![](https://img.aspxhome.com/file/2023/8/82538_0s.jpg)
Python实现动态绘图的示例详解
2021-07-08 16:44:40
![](https://img.aspxhome.com/file/2023/5/93615_0s.gif)
使用Python来编写HTTP服务器的超级指南
2022-12-28 20:40:24
![](https://img.aspxhome.com/file/2023/2/90652_0s.png)
提高MySQL数据库查询效率的三个实用技巧
2008-12-03 16:40:00
【MongoDB for Java】Java操作MongoDB数据库
2023-07-14 01:10:43
![](https://img.aspxhome.com/file/2023/4/59714_0s.jpg)
php源码的安装方法和实例
2023-07-06 03:31:37
Python Tkinter实现简易计算器功能
2023-08-08 09:53:30
![](https://img.aspxhome.com/file/2023/5/105795_0s.jpg)
Python实战之画哆啦A梦(超详细步骤)
2021-05-16 14:36:47
![](https://img.aspxhome.com/file/2023/0/80480_0s.jpg)
Python中请不要再用re.compile了
2021-08-30 23:57:51
![](https://img.aspxhome.com/file/2023/7/121657_0s.jpg)
asp如何在线压缩Access数据库?
2010-06-26 12:25:00
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
2021-10-09 10:44:58