将 cookie 加载到 Python 请求会话中时出错 [英] Error when loading cookies into a Python request session

查看:35
本文介绍了将 cookie 加载到 Python 请求会话中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 cookie 从 selenium 导出的 cookie 加载到 Python 中的请求会话中,但是当我这样做时,它返回以下错误:"'list' 对象没有属性 'extract_cookies'"

I am trying to load cookies into my request session in Python from selenium exported cookies, however when I do it returns the following error: "'list' object has no attribute 'extract_cookies'"

def load_cookies(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)

initial_state= requests.Session()
initial_state.cookies=load_cookies(time_cookie_file)
search_requests = initial_state.get(search_url)

我在任何地方看到这都应该有效,但是我的 cookie 是一个字典列表,这就是我理解的所有 cookie,以及为什么我认为这适用于 Selenium.但是由于某种原因它不适用于请求,在这方面的任何和所有帮助都会非常好,感觉就像我错过了一些明显的东西!

Everywhere I see this should work, however my cookies are a list of dictionaries, which is what I understand all cookies are, and why I assume this works with Selenium. However for some reason it does not work with requests, any and all help in this regard would be really great, it feels like I am missing something obvious!

使用以下方法从 Selenium 转储 Cookie:

Cookies have been dumped from Selenium using:

with open("Filepath.pkl", 'wb') as f:
    pickle.dump(driver.get_cookies(), f)

cookie 的一个例子是(稍微混淆):

An example of the cookies would be (slightly obfuscated):

[{'domain': '.website.com',
  'expiry': 1640787949,
  'httpOnly': False,
  'name': '_ga',
  'path': '/',
  'secure': False,
  'value': 'GA1.2.1111111111.1111111111'},
 {'domain': 'website.com',
  'expiry': 1585488346,
  'httpOnly': False,
  'name': '__pnahc',
  'path': '/',
  'secure': False,
  'value': '0'}]

我现在已经按照下面的答案设法加载了 cookie,但是 cookie 似乎没有正确加载,因为它们不记得任何东西,但是如果我在浏览 Selenium 时加载 cookie,它们就可以工作很好.

I have now managed to load in the cookies as per the answer below, however it does not seem like the cookies are loaded in properly as they do not remember anything, however if I load the cookies in when browsing through Selenium they work fine.

推荐答案

Cookie

Cookie HTTP 请求头包含存储的 HTTP cookie 先前由服务器使用 Set-Cookie 发送标题.HTTP cookie 是服务器发送的一小段数据到用户的网络浏览器.浏览器可能会存储 cookies 并将其与下一个请求一起发送回同一服务器.通常, 来判断两个请求是否来自相同的浏览器,保持用户登录.

Cookie

The Cookie HTTP request header contains stored HTTP cookie previously sent by the server with the Set-Cookie header. A HTTP cookie is a small piece of data that a server sends to the user's web browser. The browser may store the cookies and send it back with the next request to the same server. Typically, cookies to tell if two requests came from the same browser, keeping the user logged in.

使用 Selenium 我们已经使用 一旦用户存储了 cookie已登录网站 http://demo.guru99.com/test/cookie/selenium_aut.php.在下一步中,我们打开了同一个网站,添加了 cookie,并能够以登录用户的身份登陆.

To demonstrate the usage of cookies using Selenium we have stored the cookies using pickle once the user had logged into the website http://demo.guru99.com/test/cookie/selenium_aut.php. In the next step, we opened the same website, adding the cookies and was able to land as a logged in user.

  • 用于存储 cookie 的代码块:

  • Code Block to store the cookies:

from selenium import webdriver
import pickle

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')
driver.find_element_by_name("username").send_keys("abc123")
driver.find_element_by_name("password").send_keys("123xyz")
driver.find_element_by_name("submit").click()
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

  • 使用存储的 cookie 进行自动身份验证的代码块:

  • Code Block to use the stored cookies for automatic authentication:

    from selenium import webdriver
    import pickle
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
    driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')
    cookies = pickle.load(open("cookies.pkl", "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)
    driver.get('http://demo.guru99.com/test/cookie/selenium_cookie.php')
    

  • 使用 我们访问了该网站 https://www.google.com,添加了新的 cookie 字典:

    To demonstrate usage of cookies using session and requests we have accessed the site https://www.google.com, added a new dictionary of cookies:

    {'name':'my_own_cookie','value': 'debanjan' ,'domain':'.stackoverflow.com'}
    

    接下来,我们使用相同的 requests 会话发送另一个成功的请求,如下所示:

    Next, we have used the same requests session to send another request which was successful as follows:

    • 代码块:

    • Code Block:

    import requests
    
    s1 = requests.session()
    s1.get('https://www.google.com')
    print("Original Cookies")
    print(s1.cookies)
    print("==========")
    cookie = {'name':'my_own_cookie','value': 'debanjan' ,'domain':'.stackoverflow.com'}
    s1.cookies.update(cookie)
    print("After new Cookie added")
    print(s1.cookies)
    

  • 控制台输出:

  • Console Output:

    Original Cookies
    <RequestsCookieJar[<Cookie 1P_JAR=2020-01-21-14 for .google.com/>, <Cookie NID=196=NvZMMRzKeV6VI1xEqjgbzJ4r_3WCeWWjitKhllxwXUwQcXZHIMRNz_BPo6ujQduYCJMOJgChTQmXSs6yKX7lxcfusbrBMVBN_qLxLIEah5iSBlkdBxotbwfaFHMd-z5E540x02-YZtCm-rAIx-MRCJeFGK2E_EKdZaxTw-StRYg for .google.com/>]>
    ==========
    After new Cookie added
    <RequestsCookieJar[<Cookie domain=.stackoverflow.com for />, <Cookie name=my_own_cookie for />, <Cookie value=debanjan for />, <Cookie 1P_JAR=2020-01-21-14 for .google.com/>, <Cookie NID=196=NvZMMRzKeV6VI1xEqjgbzJ4r_3WCeWWjitKhllxwXUwQcXZHIMRNz_BPo6ujQduYCJMOJgChTQmXSs6yKX7lxcfusbrBMVBN_qLxLIEah5iSBlkdBxotbwfaFHMd-z5E540x02-YZtCm-rAIx-MRCJeFGK2E_EKdZaxTw-StRYg for .google.com/>]>
    

  • 显然,新添加的cookie字典 {'name':'my_own_cookie','value': 'debanjan' ,'domain':'.stackoverflow.com'} 几乎在在第二个请求中使用.

    Clearly, the newly added dictionary of cookies {'name':'my_own_cookie','value': 'debanjan' ,'domain':'.stackoverflow.com'} is pretty much in use within the second request.

    现在,如果您的用例是将 Selenium Cookie 传递给 Python 请求,您可以使用以下解决方案:

    Now, if your usecase is to passing Selenium Cookies to Python Requests, you can use the following solution:

    from selenium import webdriver
    import pickle
    import requests
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
    driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')
    driver.find_element_by_name("username").send_keys("abc123")
    driver.find_element_by_name("password").send_keys("123xyz")
    driver.find_element_by_name("submit").click()
    
    # Storing cookies through Selenium
    pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
    driver.quit()
    
    # Passing cookies to Session
    session = requests.session()  # or an existing session
    with open('cookies.pkl', 'rb') as f:
        session.cookies.update(pickle.load(f))
    search_requests = session.get('https://www.google.com/')
    print(session.cookies)
    

    这篇关于将 cookie 加载到 Python 请求会话中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆