【问题标题】:Is it possible to dump the cookes in the request headers using selenium?是否可以使用 selenium 在请求标头中转储厨师?
【发布时间】:2021-04-02 13:57:36
【问题描述】:

我的目标是使用请求标头从请求标头中获取 cookie。

我尝试过使用 request、selenium 和 seleniumwire,但我从它们得到的结果与我在浏览器(chrome)中找到的结果不同。

我试过的代码:

from seleniumwire import webdriver
import pickle, time

print("Start\n")
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('window-size=414x736')
options.add_argument('--disable-gpu')
options.add_argument('--hide-scrollbars')
options.add_argument('blink-settings=imagesEnabled=false')
options.add_argument('--headless')
options.add_argument('--enable-file-cookies')

##  Get the URL
browser = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)
browser.get("https://www.xxxxxxx.com/xxxxxx")

time.sleep(5)

print(browser.get_cookies())

browser.quit()

我也试过在python中使用requests库,结果和selenium一样。

我在浏览器工具中找到的我想从 selenium 获取的 headers 请求:

【问题讨论】:

  • 代码产生的结果是什么?

标签: python selenium cookies python-requests


【解决方案1】:

是的,

为了获取 cookie,

browser.get("https://www.example.com")
# get all the cookies from this domain
cookies = browser.get_cookies()
# store it somewhere, maybe a text file

用于恢复 cookie

browser.get("https://www.example.com")
# get back the cookies
cookies = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
browser.add_cookies(cookies)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2012-06-28
    • 1970-01-01
    • 2011-08-05
    • 2020-02-21
    • 2012-11-06
    • 1970-01-01
    • 2023-01-05
    相关资源
    最近更新 更多