获取信息门户的Cookie时,我们需要动态的获取,以得到正确的Cookie.
因为原来华师匣子课表等有数据库,故不需要此cookie,但获取成绩就需要这个Cookie了.
学长给的获取cookie的示例因为需要Python3中的async,而之前monitor的代码都是基于Python2的,故不可以直接用,需要从新写一个Python2版本的.
按照学长给的示例,需要3次Session的请求.
在三次请求之后才可以得到有正确Cookie的session.
我的代码如下(最后还是有问题)
|
|
直接print ret
1{'cookies': <RequestsCookieJar[Cookie(version=0, name='BIGipServerpool_jwc_xk', value='1028696256.20480.0000', port=None, port_specified=False, domain='122.204.187.6', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='JSESSIONID', value='320CA9B505D70D76A12D3D826BB6F20E', port=None, port_specified=False, domain='122.204.187.6', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='BIGipServerpool_portal', value='173058240.20480.0000', port=None, port_specified=False, domain='portal.ccnu.edu.cn', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='JSESSIONID', value='aaa5uKro_uGjW6WSg0MWv', port=None, port_specified=False, domain='portal.ccnu.edu.cn', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>, 'stream': False, 'hooks': {'response': []}, 'redirect_cache': <requests.packages.urllib3._collections.RecentlyUsedContainer object at 0x7fb36c78de50>, 'auth': None, 'trust_env': True, 'headers': {'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.13.0'}, 'cert': None, 'params': {}, 'verify': True, 'proxies': {}, 'adapters': OrderedDict([('https://', <requests.adapters.HTTPAdapter object at 0x7fb36ca53790>), ('http://', <requests.adapters.HTTPAdapter object at 0x7fb36ca53b90>)]), 'max_redirects': 30}pprint ret
12345678910111213{'adapters': OrderedDict([('https://', <requests.adapters.HTTPAdapter object at 0x7fb36ca53790>), ('http://', <requests.adapters.HTTPAdapter object at 0x7fb36ca53b90>)]),'auth': None,'cert': None,'cookies': <RequestsCookieJar[Cookie(version=0, name='BIGipServerpool_jwc_xk', value='1028696256.20480.0000', port=None, port_specified=False, domain='122.204.187.6', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='JSESSIONID', value='320CA9B505D70D76A12D3D826BB6F20E', port=None, port_specified=False, domain='122.204.187.6', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='BIGipServerpool_portal', value='173058240.20480.0000', port=None, port_specified=False, domain='portal.ccnu.edu.cn', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='JSESSIONID', value='aaa5uKro_uGjW6WSg0MWv', port=None, port_specified=False, domain='portal.ccnu.edu.cn', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>,'headers': {'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.13.0'},'hooks': {'response': []},'max_redirects': 30,'params': {},'proxies': {},'redirect_cache': <requests.packages.urllib3._collections.RecentlyUsedContainer object at 0x7fb36c78de50>,'stream': False,'trust_env': True,'verify': True}
打印<class 'requests.cookies.RequestsCookieJar'>
的key,value:1234BIGipServerpool_jwc_xk 1028696256.20480.0000JSESSIONID 320CA9B505D70D76A12D3D826BB6F20EBIGipServerpool_portal 173058240.20480.0000JSESSIONID aaa5uKro_uGjW6WSg0MWv
本文地址: http://Humbertzhang.github.io/2017/05/21/模拟登录下获取Cookie/