Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Selenium中获取请求头_Python_Selenium_Fiddler - Fatal编程技术网

Python 如何在Selenium中获取请求头

Python 如何在Selenium中获取请求头,python,selenium,fiddler,Python,Selenium,Fiddler,如果您打开它的“匿名”窗口并检查Fiddler中的标题,则会得到以下两个主要标题: 当我点击最后一个并检查请求头时,我得到的就是这个 我想用Python获取这些标题。有什么方法可以用硒来获得这些吗?我在这里有点不知所措 使用Selenium无法获取标题 但是,您可以使用其他库,例如请求,美化组,来获取标题。底线是,否,您无法使用检索请求标题 细节 Selenium用户一直要求添加从HTTP响应读取HTTP状态代码和头的方法。我们在讨论中详细讨论了如何通过Selenium实现此功能 然而,J

如果您打开它的“匿名”窗口并检查Fiddler中的标题,则会得到以下两个主要标题:

当我点击最后一个并检查请求头时,我得到的就是这个


我想用Python获取这些标题。有什么方法可以用硒来获得这些吗?我在这里有点不知所措

使用Selenium无法获取标题


但是,您可以使用其他库,例如
请求
美化组
,来获取标题。

底线是,,您无法使用检索请求标题


细节 Selenium用户一直要求添加从HTTP响应读取HTTP状态代码和头的方法。我们在讨论中详细讨论了如何通过Selenium实现此功能

然而,Jason Leyba(硒贡献者)在他的文章中直截了当地提到:

我们不会将此功能添加到WebDriver API,因为它超出了我们当前的范围(模拟用户操作)

Ashley Leyba进一步补充说,试图使WebDriver成为理想的web测试工具将在总体质量上受到影响,因为
driver.get(url)
会一直阻塞,直到浏览器加载页面并返回最终加载页面的响应。所以,在登录重定向的情况下,状态代码和标题总是以200结尾,而不是您要查找的302

最后,Simon M Stewart(WebDriver创建者)在他的报告中得出结论:

这项功能不会出现。建议的方法是扩展以访问所需的信息,或者使用公开此信息的外部代理,例如


你可以用硒丝。它是一种硒延伸,正是为了这个目的而开发的

pip安装后的示例:

https://www.sahibinden.com/en
##从Selenium Wire而不是Selenium导入webdriver
从seleniumwire导入webdriver
##获取URL
driver=webdriver.Chrome(“my/path/to/driver”,options=options)
驱动程序。获取(“https://my.test.url.com")
##打印请求标题
对于driver.requests中的请求:

print(request.url)#您可以像这样运行JS命令

##  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
关于Python

var req = new XMLHttpRequest()
req.open('GET', document.location, false)
req.send(null)
return req.getAllResponseHeaders()

也许你可以用BrowserMob代理来做这个。以下是一个例子:

driver.get("https://t.me/codeksiyon")
headers = driver.execute_script("var req = new XMLHttpRequest();req.open('GET', document.location, false);req.send(null);return req.getAllResponseHeaders()")

# type(headers) == str

headers = headers.splitlines()

这些标题仅在第一次访问站点时显示在icognito窗口中。然后会存储cookies,并且不会访问此页面。BS是否能够在每次运行时捕获这些标题?您是否可以共享任何资源?您可以使用请求获取HTML内容和标题。默认情况下,它不会缓存。()然后,如果需要,您可以使用BeautifulSoup解析此HTML。我需要请求头而不是响应头我可以拦截对url的请求吗?您可能知道ruby是否存在类似的请求吗?:)不幸的是,我只在Python中使用了selenium:/
import settings

from browsermobproxy import Server
from selenium.webdriver import DesiredCapabilities

config = settings.Config

server = Server(config.BROWSERMOB_PATH)
server.start()
proxy = server.create_proxy()

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxy.proxy)
chrome_options.add_argument('--headless')

capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptSslCerts'] = True
capabilities['acceptInsecureCerts'] = True

driver = webdriver.Chrome(options=chrome_options,
    desired_capabilities=capabilities,
   executable_path=config.CHROME_PATH)

proxy.new_har("sahibinden", options={'captureHeaders': True})
driver.get("https://www.sahibinden.com/en")

entries = proxy.har['log']["entries"]
for entry in entries:
    if 'request' in entry.keys():
        print(entry['request']['url'])
        print(entry['request']['headers'])
        print('\n')

proxy.close()
driver.quit()