Using Chrome's cookies in Python-Requests

前端 未结 2 1839
温柔的废话
温柔的废话 2021-02-19 00:36

I\'m trying to log in to the http://www.steampowered.com website using the cookies I\'ve got from my Chrome session.

Once I\'ve grabbed all the cookie tabl

相关标签:
2条回答
  • 2021-02-19 01:12

    Keep in mind you're trying to do something that the HTTP specification intentionally tries to prevent you from doing (i.e. sending cookies to domains that they didn't come from). So you might be doomed from the start. And to make matters worse for you, I took a cursory look at the way steampowered implements login and you have your work cut out for you.

    Back to your question...

    Now, assuming your steampowered session cookies are valid (which they might not be based on the encryption, key sharing and captcha methods the login page performs), you might be able to login with the requests library by simply supplying a valid cookie dict as the docs state.

    my_cookies = {'cookiename1': 'cookievalue1', 'cookiename2': 'cookievalue2'}
    response = requests.get(
        'http://www.steampowered.com/mystuff',
        cookies=my_cookies)
    

    Also, I don't know what data is stored in the databases you're getting the cookies from, but keep in mind that they might be storing all the metadata that comes along with a 'Set-Cookie' header (expiry, path, domain, etc). That's information the user-agent (Chrome, IE, the requests library, etc) uses to determine which cookies to send in a request but it is not included in the request. A 'Cookie' header only has name=value pairs. So that's all you need to supply in your cookie dict.

    And, if you have two cookies with the same name, just pick one. Because in the end, most likely only one will be evaluated or else the server will simply return an error.

    0 讨论(0)
  • 2021-02-19 01:18

    I created a module to load cookies from Firefox.

    Example usage with requests:

    import requests
    import browser_cookie
    cj = browser_cookie.firefox()
    r = requests.get(url, cookies=cj)
    
    0 讨论(0)
提交回复
热议问题