Using Chrome's cookies in Python-Requests

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:26:40
hoju

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!