Python 更改硒驱动程序的用户代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29916054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:09:52  来源:igfitidea点击:

Change user agent for selenium driver

pythonseleniumhttp-headersuser-agent

提问by xralf

I have the following code in Python:

我有以下代码Python

from selenium.webdriver import Firefox
from contextlib import closing

with closing(Firefox()) as browser:
  browser.get(url)

I would like to print the user-agent HTTP header and possibly change it. Is it possible?

我想打印用户代理 HTTP 标头并可能更改它。是否可以?

采纳答案by Louis

There is no way in Selenium to read the request or response headers. You could do it by instructing your browser to connect through a proxy that records this kind of information.

Selenium 无法读取请求或响应标头。您可以通过指示浏览器通过记录此类信息的代理进行连接来实现。

Setting the User Agent in Firefox

在 Firefox 中设置用户代理

The usual way to change the user agent for Firefox is to set the variable "general.useragent.override"in your Firefox profile. Note that this is independent from Selenium.

更改 Firefox 用户代理的常用方法是"general.useragent.override"在您的 Firefox 配置文件中设置变量。请注意,这独立于 Selenium。

You can direct Selenium to use a profile different from the default one, like this:

您可以指示 Selenium 使用与默认配置不同的配置文件,如下所示:

from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "whatever you want")
driver = webdriver.Firefox(profile)

Setting the User Agent in Chrome

在 Chrome 中设置用户代理

With Chrome, what you want to do is use the user-agentcommand line option. Again, this is not a Selenium thing. You can invoke Chrome at the command line with chrome --user-agent=footo set the agent to the value foo.

对于 Chrome,您要做的是使用user-agent命令行选项。同样,这不是 Selenium 的事情。您可以在命令行调用 Chrome 以chrome --user-agent=foo将代理设置为值foo

With Selenium you set it like this:

使用 Selenium,你可以这样设置:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("user-agent=whatever you want")

driver = webdriver.Chrome(chrome_options=opts)

Both methods above were tested and found to work. I don't know about other browsers.

以上两种方法都经过测试并发现有效。我不知道其他浏览器。

Getting the User Agent

获取用户代理

Selenium does not have methods to query the user agent from an instance of WebDriver. Even in the case of Firefox, you cannot discover the default user agent by checking what general.useragent.overridewould be if not set to a custom value. (This setting does not existbefore it is set to some value.)

Selenium 没有从 的实例查询用户代理的方法WebDriver。即使在 Firefox 的情况下,您也无法通过检查general.useragent.override如果未设置为自定义值会是什么来发现默认用户代理。(此设置在设置为某个值之前不存在。)

Once the browser is started, however, you can get the user agent by executing:

但是,一旦浏览器启动,您就可以通过执行以下命令来获取用户代理:

agent = driver.execute_script("return navigator.userAgent")

The agentvariable will contain the user agent.

agent变量将包含用户代理。

回答by JJC

To build on Louis's helpful answer...

以路易斯的有用回答为基础......

Setting the User Agent in PhantomJS

在 PhantomJS 中设置用户代理

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
...
caps = DesiredCapabilities.PHANTOMJS
caps["phantomjs.page.settings.userAgent"] = "whatever you want"
driver = webdriver.PhantomJS(desired_capabilities=caps)

The only minor issue is that, unlike for Firefox and Chrome, this does notreturn your custom setting:

唯一的小问题是,不像Firefox和Chrome,这并没有返回您的自定义设置:

driver.execute_script("return navigator.userAgent")

So, if anyone figures out how to do that in PhantomJS, please edit my answer or add a comment below! Cheers.

所以,如果有人想出如何在 PhantomJS 中做到这一点,请编辑我的答案或在下面添加评论!干杯。

回答by J. Does

To build on JJC's helpful answer that builds on Louis's helpful answer...

以 JJC 的有用答案为基础,该答案建立在 Louis 的有用答案之上...

With PhantomJS 2.1.1-windows this line works:

使用 PhantomJS 2.1.1-windows,这条线有效:

driver.execute_script("return navigator.userAgent")

If it doesn't work, you can still get the user agent via the log (to build on Mma's answer):

如果它不起作用,您仍然可以通过日志获取用户代理(以Mma 的回答为基础):

from selenium import webdriver
import json
from fake_useragent import UserAgent

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (UserAgent().random)
driver = webdriver.PhantomJS(executable_path=r"your_path", desired_capabilities=dcap)
har = json.loads(driver.get_log('har')[0]['message']) # get the log
print('user agent: ', har['log']['entries'][0]['request']['headers'][1]['value'])

回答by Nioooooo

This is a short solution to change the request UserAgent on the fly.

这是动态更改请求 UserAgent 的简短解决方案。

Change UserAgent of a request with Chrome

使用 Chrome 更改请求的 UserAgent

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Chrome(driver_path)
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"python 2.7", "platform":"Windows"})
driver.get('http://amiunique.org')

then return your useragent:

然后返回您的用户代理:

agent = driver.execute_script("return navigator.userAgent")

Some sources

一些来源

The source code of webdriver.pyfrom SeleniumHQ (https://github.com/SeleniumHQ/selenium/blob/11c25d75bd7ed22e6172d6a2a795a1d195fb0875/py/selenium/webdriver/chrome/webdriver.py) extends its functionalities through the Chrome Devtools Protocol

来自 SeleniumHQ ( https://github.com/SeleniumHQ/selenium/blob/11c25d75bd7ed22e6172d6a2a795a1d195fb0875/py/selenium/webdriver/chrome/webdriver.py)的webdriver.py源代码通过 Chrome Devtools 协议扩展其功能

def execute_cdp_cmd(self, cmd, cmd_args):
        """
        Execute Chrome Devtools Protocol command and get returned result

We can use the Chrome Devtools Protocol Viewer to list more extended functionalities (https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setUserAgentOverride) as well as the parameters type to use.

我们可以使用 Chrome Devtools 协议查看器来列出更多扩展功能(https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setUserAgentOverride)以及要使用的参数类型。