【问题标题】:How to change the displayed operating system information within the useragent using Selenium and Python?如何使用 Selenium 和 Python 更改用户代理中显示的操作系统信息?
【发布时间】:2020-11-05 18:22:39
【问题描述】:

我正在构建一个机器人,它将根据我选择的随机用户代理来改变它的外观。我从用户代理列表中提取,根据选择的用户代理,我更改 webdriver 的功能,然后启动它。

但是,我使用https://ipleak.net/ 进行测试,并且无论我运行此代码多少次,我的真实操作系统仍然显示(而不是所谓的欺骗版本)。下面的相关代码,FYI true OS = MacIntel

desired_caps = DesiredCapabilities.CHROME.copy()
desired_caps['platform'] = user_agent_os

browser = webdriver.Chrome(options=options, desired_capabilities=desired_caps)
browser.get("https://ipleak.net/")

user_agent_os = 'ios'、'windows'、'andriod'、'---'、'symbian'、'macos'、'linux' 等的可能值。每次程序运行时,该值都是随机的。

【问题讨论】:

  • 我想知道是否可以对标头使用 Selenium-Wire 做一些花哨的事情,但我认为这不会改变 webdriver 的功能。
  • 另外我认为标题具有误导性,我想更改platform 中的操作系统信息以匹配user-agent,而不是相反

标签: python-3.x selenium selenium-webdriver selenium-chromedriver user-agent


【解决方案1】:

platform 属性可以从WebDriver navigator 中提取。

您可以使用以下代码行来提取navigator.platform 和我的 系统上的值:

  • 代码块:

    print("platform: "+driver.execute_script("return navigator.platform;"))
    
  • 控制台输出:

    platform: Win32
    

Navigator platform Property 的可能值为:

  • Mac68K
  • MacPPC
  • MacIntel
  • Linux i686
  • Linux armv7l
  • Win32
  • Win16
  • WinCE
  • SunOS
  • HP-UX

另一方面,UserAgentnavigator.userAgent 确实包含 信息:

  • 代码块:

    print("userAgent: "+driver.execute_script("return navigator.userAgent;"))
    
  • 控制台输出:

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

解决方案

UserAgent 中更改 信息的唯一方法是使用Python 的fake_useragent 模块旋转UserAgent

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

options = Options()
ua = UserAgent()
options.add_argument(f'user-agent={ua.random}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
print("userAgent: "+driver.execute_script("return navigator.userAgent;"))
driver.quit()

连续3次执行结果如下:

  1. 第一次执行:

    userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20130331 Firefox/21.0
    
  2. 第二次执行:

    userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
    
  3. 第三次执行:

    userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36
    

火狐

from selenium import webdriver
from fake_useragent import UserAgent

useragent = UserAgent()
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", useragent.random)
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'C:\WebDrivers\\geckodriver.exe')
print("userAgent: "+driver.execute_script("return navigator.userAgent;"))
driver.quit()

连续3次执行结果如下:

  1. 第一次执行:

    userAgent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
    
  2. 第二次执行:

    userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17
    
  3. 第三次执行:

    userAgent: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36
    

tl;博士

您可以在以下位置找到一些关于即时更改 useragent 的相关讨论:

【讨论】:

  • 感谢您的回复,非常详细,非常感谢。我真正追求的是如何将我的计算机显示的platform值更改为每个网站。希望将其与 useragent 标头中的 os 匹配。
  • @Heartthrob_Rob 这正是我解释了如何在useragent 标头中欺骗platform 值。
  • @Heartthrob_Rob 如果我的answer满足了您的问题,请点击空心处acceptanswer我的 answer 旁边的复选标记位于 votedown 箭头下方,因此复选标记变为 绿色
  • 嗨@DebanjanB,我想我上面可能不清楚。我正在从我提取的列表中随机化useragent。然后我想将我的 platform 值与这个 user_agent 匹配。我相信你上面的代码与我的useragent 匹配到platform,我在后面。
猜你喜欢
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多