如何在Selenium中获取请求标头
问题描述:
https://www.sahibinden.com/en
如果您打开隐身窗口并在Fiddler中检查标题,那么您将获得以下两个主要标题:
If you open it incognito window and check headers in Fiddler then these are the two main headers you get:
当我单击最后一个并检查请求标头时,这就是我得到的
When I click the last one and check request headers this is what I get
我想在Python中获得这些标头.有什么办法可以使用Selenium来获得这些东西吗?我在这里有点无能为力.
I want to get these headers in Python. Is there any way that I can get these using Selenium? Im a bit clueless here.
答
您可以使用Selenium Wire.这是为此目的而开发的Selenium扩展程序.
You can use Selenium Wire. It is a Selenium extension which has been developed for this exact purpose.
https://pypi.org/project/selenium-wire/
pip安装后的示例:
An example after pip install:
## Import webdriver from Selenium Wire instead of Selenium
from seleniumwire import webdriver
## Get the URL
driver = webdriver.Chrome("my/path/to/driver", options=options)
driver.get("https://my.test.url.com")
## Print request headers
for request in driver.requests:
print(request.url) # <--------------- Request url
print(request.headers) # <----------- Request headers
print(request.response.headers) # <-- Response headers