简体   繁体   English

如何使用 Selenium 和 Python 为用户代理设置自定义名称

[英]How to set a custom name for the user-agent using Selenium and Python

I am using selenium + webdriver and trying testing different user agents.我正在使用 selenium + webdriver 并尝试测试不同的用户代理。 I am adding user agent like this for Chrome on Windows for example:我正在为 Windows 上的 Chrome 添加这样的用户代理,例如:

option = Options()
option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")

Now when I log see login details it says Windows Chrome but when I want to rename it to something else like this:现在,当我登录查看登录详细信息时,它显示 Windows Chrome 但是当我想将其重命名为类似这样的其他名称时:

option.add_argument("user-agent=test-user-agent")

or或者

option.add_argument("user-agent=Mozilla/5.0 (test-user-agent NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")

Some websites display it as unknown or browser not supported某些网站显示为unknownbrowser not supported

Is there a way to "rename" user-agent or create custom one or there is only preset number of them that websites know?有没有办法“重命名”用户代理或创建自定义用户代理,或者只有网站知道的预设数量?

User-Agent用户代理

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent . 用户代理请求 header是一个特征字符串,它允许服务器和网络对等方识别请求用户代理的应用程序、操作系统、供应商和/或版本。


Syntax句法

The common format for web browsers is as follows: web浏览器常用格式如下:

User-Agent: Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>

This usecase这个用例

While your first code attempt to add a specific would work perfect:虽然您尝试添加特定的第一个代码将完美运行:

  • Code Block:代码块:

     from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36") driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe') print(driver.execute_script("return navigator.userAgent;"))
  • Console Output:控制台 Output:

     Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36

But as per your second attempt you can't rename the User-Agent as it violates the prescribed format/syntax.但是根据您的第二次尝试,您无法重命名用户代理,因为它违反了规定的格式/语法。


However, you can always change the User-Agent using the execute_cdp_cmd(cmd, cmd_args) as follows:但是,您始终可以使用execute_cdp_cmd(cmd, cmd_args)更改用户代理,如下所示:

  • Code Block:代码块:

     from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36") driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe') print(driver.execute_script("return navigator.userAgent;")) # Setting UserAgent as Chrome/83.0.4103.97 driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}) print(driver.execute_script("return navigator.userAgent;"))
  • Console Output:控制台 Output:

     Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36

References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

Warning: FirefoxProfile is DEPRICATED警告:FirefoxProfile 已被删除

The normal method of using FirefoxProfile is now depricated and you must use Options instead.使用FirefoxProfile的常规方法现已弃用,您必须改用Options

How to change FireFox User Agent using Options如何使用选项更改 FireFox 用户代理

Simply do the same as FirefoxProfile but with Options object instead like so:只需执行与FirefoxProfile相同的操作,但使用Options object 代替如下:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
agent = " Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
options.set_preference("general.useragent.override", agent)

driver = webdriver.Firefox(options=options)

Using random User Agent from git使用来自 git 的随机用户代理

To use a random user agent you can just pull from this git list of useragents at random (requires pip install requests ):要使用随机用户代理,您可以从用户代理的 git 列表中随机提取(需要pip install requests ):

import requests,random
agents = requests.get("https://gist.githubusercontent.com/pzb/b4b6f57144aea7827ae4/raw/cf847b76a142955b1410c8bcef3aabe221a63db1/user-agents.txt").text.split('\n')
agent = random.choice(agents)

Using random user agent from pip package使用来自 pip package 的随机用户代理

There is a helpful package called random-user-agent by installing through pip有一个有用的 package 通过安装 pip 称为random-user-agent

pip install random-user-agent

Then to get a random agent:然后得到一个随机代理:

from fake_useragent import UserAgent
agent = UserAgent().random

Full Example完整示例

Here is a full example of setting a random user agent for selenium using options:这是使用选项为 selenium 设置随机用户代理的完整示例:

from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("general.useragent.override", UserAgent().random)

driver = webdriver.Firefox(options=options)

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python MozEmbed中设置User-Agent? - How to set User-Agent in Python MozEmbed? 如何在用 Python 编写的 Selenium 脚本中成功更改 User-Agent? - How to successfully change User-Agent in Selenium script written in Python? 如何在 python-webkit 中设置用户代理 - How to set user-agent in python-webkit 如何在python-twitter中设置User-Agent? - How to set User-Agent in python-twitter? 如何使用Gio设置HTTP请求的用户代理? - How to set the user-agent of an HTTP request using Gio? 访问被拒绝 - python selenium - 即使在使用用户代理和其他标头之后 - Access denied - python selenium - even after using User-Agent and other headers 与移动用户代理一起使用时,无法单击使用 python selenium 的网页元素 - Can't click on webpage element using python selenium when used with mobile user-agent 如何使用 Python 在 selenium 中编辑#shadow-root(用户代理)值 - How to edit #shadow-root (user-agent) value in selenium with Python 使用 selenium chromedriver 将联系信息添加到用户代理 - Add contact information to user-agent using selenium chromedriver 使用selenium和phantomJS更改“user-agent”标头 - Changing the “user-agent” header using selenium with phantomJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM