使用名为 "requests" 的 python 库从网页请求 cookie 后,我们在字符串中存储的信息是什么?
what are the information stored in strings we get after requesting cookies from a webpage while using python libraray called "requests"?
实际上我正在浏览名为 requests
的 python 图书馆。在向网页发出获取请求并将其响应存储在名为 r
的对象中之后;当我试图查看该对象的 cookie 内容时;我在 <..>
中关闭了字符串,主要包含随机字母数字字符。我无法获得或理解该字符串中隐藏的信息。
import requests
r_object = requests.get('https://www.stackexchange.com/stories')
print (r_object.cookies)
这是它的输出:
<RequestsCookieJar[<Cookie prov=11531d97-98e5-3af7-2bdf-76e3edeece3e for .stackexchange.com/>]>
我想你想做的是:
import requests
with requests.Session() as s:
resp = s.get("https://github.com")
print(resp.cookies)
# 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/>]>
请记住,您还有两个其他属性可以帮助您处理 cookie:
resp.cookies.items()
resp.cookies.get_dict()
也可以用正则提取:
- Example 1
- Example 2
这是一个例子:
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')]]
实际上我正在浏览名为 requests
的 python 图书馆。在向网页发出获取请求并将其响应存储在名为 r
的对象中之后;当我试图查看该对象的 cookie 内容时;我在 <..>
中关闭了字符串,主要包含随机字母数字字符。我无法获得或理解该字符串中隐藏的信息。
import requests
r_object = requests.get('https://www.stackexchange.com/stories')
print (r_object.cookies)
这是它的输出:
<RequestsCookieJar[<Cookie prov=11531d97-98e5-3af7-2bdf-76e3edeece3e for .stackexchange.com/>]>
我想你想做的是:
import requests
with requests.Session() as s:
resp = s.get("https://github.com")
print(resp.cookies)
# 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/>]>
请记住,您还有两个其他属性可以帮助您处理 cookie:
resp.cookies.items()
resp.cookies.get_dict()
也可以用正则提取:
- Example 1
- Example 2
这是一个例子:
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')]]