简体   繁体   English

在Python-Requests中使用Chrome的Cookie

[英]Using Chrome's cookies in Python-Requests

I'm trying to log in to the http://www.steampowered.com website using the cookies I've got from my Chrome session. 我正在尝试使用我从Chrome会话中获得的Cookie登录http://www.steampowered.com网站。

Once I've grabbed all the cookie table's data, using the command SELECT * FROM cookie WHERE host_key LIKE '%steam%' and the column names: PRAGMA table_info(cookie) and sorted through all the data with a list comprehension, I don't know how to pass it all to requests so that the cookies become usable. 一旦我抓住了所有cookie表的数据,使用命令SELECT * FROM cookie WHERE host_key LIKE '%steam%'和列名: PRAGMA table_info(cookie)并通过列表理解对所有数据进行排序,我不知道知道如何将所有内容传递给requests以便cookie可用。

The request 's docs say you need to pass in a dict, ie cookies={'cookies':'are_working'} but then some of the keys name s overwrite each other, since a few of the name s are : Steam_Language , though they're different hosts. request文档说你需要传入一个字典,即cookies={'cookies':'are_working'}但是然后一些键name相互覆盖,因为一些name是: Steam_Language ,虽然他们是不同的主人。

edit: Just found How to add cookie to existing cookielib CookieJar instance in Python? 编辑:刚刚找到如何在Python中将cookie添加到现有的cookielib CookieJar实例? which might help me out, but I don't know how to format the Chrome cookies for cookielib 这可能会帮助我,但我不知道如何格式化cookielib的Chrome Cookie

My question is: How do I pass several different sites worth of cookies to requests ? 我的问题是:如何将几个不同网站的cookie传递给requests

I created a module to load cookies from Firefox . 我创建了一个模块来从Firefox加载cookie

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 (ie sending cookies to domains that they didn't come from). 请记住,您正在尝试执行HTTP规范有意阻止您执行的操作(即将cookie发送到他们并非来自的域)。 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. 为了让事情变得更糟,我粗略地看一下steampowered实现登录的方式,并为你完成工作。

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. 现在,假设您的steampowered会话cookie有效(它们可能不是基于登录页面执行的加密,密钥共享和验证码方法),您可以通过简单地提供有效的cookie dict作为登录请求库。 docs说。

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). 此外,我不知道您从中获取cookie的数据库中存储了哪些数据,但请记住,他们可能正在存储随“Set-Cookie”标题一起提供的所有元数据(到期,路径,域等)。 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. 这是用户代理(Chrome,IE,请求库等)用于确定在请求中发送哪些cookie但是它未包含在请求中的信息。 A 'Cookie' header only has name=value pairs. 'Cookie'标头只有名称=值对。 So that's all you need to supply in your cookie dict. 这就是你需要提供的所有cookie dict。

And, if you have two cookies with the same name, just pick one. 而且,如果您有两个同名的cookie,只需选择一个。 Because in the end, most likely only one will be evaluated or else the server will simply return an error. 因为最终,很可能只评估一个,否则服务器将只返回错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM