如何使用 Selenium 和 Python 更改用户代理

How to change the User Agent using Selenium and Python

我在 Python 中使用 selenium 更改 Web 驱动程序用户代理时遇到错误。

这是我的代码:

import requests
import json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Chrome(driver_path) 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'})
#Error is on line above

这是我的错误:

>>> driver = webdriver.Chrome(driver_path) driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"python 3.7", "platform":"Windows"}) 
  File "<stdin>", line 1
    driver = webdriver.Chrome(driver_path) driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"python 3.7", "platform":"Windows"})```

您应该使用驱动程序选项:

from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("user-agent=[user-agent string]")

driver = webdriver.Chrome(executable_path='path', chrome_options=options)

您的代码完美。您只需编写一行代码来更改下一行中的 。例如:

  • 代码块:

    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
    print(driver.execute_script("return navigator.userAgent;"))
    # Setting user agent 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;"))
    # Setting user agent as Chrome/83.0.4103.53
    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.53 Safari/537.36'})
    print(driver.execute_script("return navigator.userAgent;"))
    driver.get('https://www.httpbin.org/headers')
    
  • 控制台输出:

    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
    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36
    
  • 浏览器快照:


参考

您可以在以下位置找到一些相关的详细讨论:

按照以下步骤操作:

1- 您可以在每个使用它的请求中使用 produce fake user-agent 库

添加到代码:

from fake_useragent import UserAgent

2- 然后在终端中执行此操作:

 pip install fake_useragent

3- 在代码中使用:例如

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'user-agent={userAgent}')
ua = UserAgent()
userAgent = ua.random
print(userAgent)
driver = webdriver.Chrome(options=chrome_options,executable_path=r"strin path 
chrome driver")
                 

如果您想从静态 user-agent 使用此代码:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows NT 6.1; 
WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"')
driver = webdriver.Chrome(chrome_options=chrome_options)