简体   繁体   English

单独获取Cookie-Python请求

[英]Get cookies separately as it is - Python Requests

So I am trying to write up a small script with the requests library that makes a request to a site (eg. github.com), and parses the cookies in the response headers. 因此,我试图用requests库编写一个小的脚本,该脚本向站点(例如github.com)发出请求,并解析响应头中的cookie。 So when you make a request to github.com, there are 3 different Set-Cookie headers as: 因此,当您向github.com请求时,有3种不同的Set-Cookie标头:

Set-Cookie: has_recent_activity=1; path=/; expires=Thu, 27 Dec 2018 07:54:16 -0000
Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Mon, 27 Dec 2038 06:54:16 -0000; secure; HttpOnly
Set-Cookie: _gh_sess=MldFM3p...; path=/; secure; HttpOnly

Now, when you make a request via the requests API and check the Set-Cookie header via req.headers.get('Set-Cookie') , all those cookie values get clumped into one as: 现在,当您通过requests API发出requests并通过req.headers.get('Set-Cookie')检查Set-Cookie标头时,所有这些cookie值都将汇总为一个:

has_recent_activity=1; path=/; expires=Thu, 27 Dec 2018 07:54:16 -0000, logged_in=no; domain=.github.com; path=/; expires=Mon, 27 Dec 2038 06:54:16 -0000; secure; HttpOnly, _gh_sess=MldFM3p...; path=/; secure; HttpOnly

So my question is how can I obtain 3 distinctly separate intact cookies as it was sent by the server alongwith all cookie metadata information (maybe in form of a list)? 因此,我的问题是,如何获取服务器发送的3个完全独立的完整cookie以及所有cookie元数据信息(也许以列表形式)?

I am a novice to Python, so any help will be highly appreciated. 我是Python的新手,因此非常感谢您的帮助。 Cheers! 干杯!

Honestly, i can't understand what you desire to know in question comments, but if you want a way to solve the question below, it will be easy. 老实说,我无法理解您想在问题评论中了解的内容,但是如果您想要一种解决以下问题的方法,这将很容易。

So my question is how can I obtain 3 distinctly separate cookies as it was sent by the server (maybe in form of a list)? 所以我的问题是,如何获得服务器发送的3个截然不同的cookie(也许以列表形式)?

import requests
with requests.Session() as s:
    resp = s.get("https://github.com")
    print(resp.cookies)
    #resp.cookies.items()
    #resp.cookies.get_dict()
    # More details: http://docs.python-requests.org/en/master/_modules/requests/cookies/

<RequestsCookieJar[
<Cookie logged_in=no for .github.com/>,
<Cookie _gh_sess=UHd5aUZ0ZXlBVDVPMitaVVBaWFp0c1p6dFA0TWVSanJzRGgrbU1XbVkxV3VXRW9LeWgwWHpWZ2pOOHFxZmtGaTZpRExpT2NjTHRyK3hHZG5GZjlxTzllbklqK0thQytHYi9HZWsrZ1poZ1ZUakJkRU9OZmJINEh3QUR2N3h3UUh6aVdFTmFCRHlHcVpwWHo1bEM5d25adnhUemJ6Y3pFMUxTbk50Q0M0UUJrVG5hR3kxRUVoUTB2TjdUc2hWbHk3cDJDWUZ4UW85NVRuR09keFJRTlc1QT09LS1RUnZHWUpsQ3BQU0hPZGtsWDAxQXFBPT0%3D--d2bd04e94c369f425fb7e9cc57b5b5499909b140 for github.com/>,
<Cookie has_recent_activity=1 for github.com/>]>

Also it can extract by regex( regex101_A regex101_B ) 也可以通过regex( regex101_A regex101_B )提取

import requests, re
with requests.Session() as s:
    resp = s.get("https://github.com")
    show_cookie = lambda x: [re.findall(r"([^,;\s]*?=.*?(?=;|$))|(\w+(?=;|$|,))",cookie) for cookie in re.findall(r"((?:^|,\s).*?)(?=,\s\S+;|$)",x)]
    print(show_cookie(resp.headers.get('Set-Cookie')))
[[('has_recent_activity=1', ''), ('path=/', ''), ('expires=Sat, 29 Dec 2018 14:43:45 -0000', '')], [('logged_in=no', ''), ('domain=.github.com', ''), ('path=/', ''), ('expires=Wed, 29 Dec 2038 13:43:45 -0000', ''), ('', 'secure'), ('', 'HttpOnly')], [('_gh_sess=eHBNWkZscHFMeXJ3NEJUU0VXZlBQaHg0S01rby9MK24xNnFvR3gvVTBsOUJjTWNWenJPZ0RRdk9RNE9ZV2V0MTQ1bTg2NEduY3phSWRrd3l0L252KzBJNkRYZlpjWXh5c2NBZktkWGFsdjZDbEJjTEdhVmZ0YnpldDFHTEpuQzFTcDNNS21sT3BRaHhBVUFqTHQ1cDZyQWNPU005ODY0bFh0MGxCbWI5d2kwait5RlcvVjlUc2FwTTdNRE8wOHZQb0RGak5YbG1ZSDJTM2ZpQmVUUkkrdz09LS11M0ZHem1YYjdWYkVLaWtRMkhscW5nPT0%3D--f778e2d24e96f3386a2da36e2d33d2b73418deed', ''), ('path=/', ''), ('', 'secure'), ('', 'HttpOnly')]]

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

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