[英]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
某些网站显示为
unknown
或browser 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?有没有办法“重命名”用户代理或创建自定义用户代理,或者只有网站知道的预设数量?
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是一个特征字符串,它允许服务器和网络对等方识别请求用户代理的应用程序、操作系统、供应商和/或版本。
The common format for web browsers is as follows: web浏览器常用格式如下:
User-Agent: Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>
While your first code attempt to add a specific user-agent 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
You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:
The normal method of using FirefoxProfile
is now depricated and you must use Options
instead.使用
FirefoxProfile
的常规方法现已弃用,您必须改用Options
。
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)
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)
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
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.