获取信息门户的Cookie时,我们需要动态的获取,以得到正确的Cookie.
因为原来华师匣子课表等有数据库,故不需要此cookie,但获取成绩就需要这个Cookie了.
学长给的获取cookie的示例因为需要Python3中的async,而之前monitor的代码都是基于Python2的,故不可以直接用,需要从新写一个Python2版本的.
按照学长给的示例,需要3次Session的请求.
在三次请求之后才可以得到有正确Cookie的session.

我的代码如下(最后还是有问题)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
from pprint import pprint
info_login_url = "http://portal.ccnu.edu.cn/loginAction.do"
link_url = "http://portal.ccnu.edu.cn/roamingAction.do?appId=XK"
login_ticket_url = "http://122.204.187.6/xtgl/login_tickitLogin.html"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
}
post_data = {
'userName': 2016210942,
'userPass': 130395
} #将URL与header定义好
s = requests.Session() #使用 requests 的 session类
r = s.post(info_login_url,data = post_data,header=header)  #发送第一次请求
if r.text.split('"')[1] == 'index_jg.jsp': #判断是否登录成功
r_second = s.get(link_url,timeout = 4) #第二次
r_third = s.get(login_ticket_url,timeout = 4) #第三次
ret = s.__dict__ #获得s对象的所有属性
pprint(ret) #在这里可以用pprint 打印出来.pprint可以将字典等类型比较清晰地打印出来.
#然后会比较容易发现s对象有一个叫做 cookies 的键.
cookies = ret['cookies'] #在这里可以type(cookies) ,发现其为<class 'requests.cookies.RequestsCookieJar'>
#这时我们就可以看一下requests关于cookie的文档
#找到这个类,发现虽然它就像一个字典一样
#打印它的key,value,就可以比较容易发现我们需要的数据了
for key,value in cookies.iteritems():
print key,value
jwcinfo = cookies['BIGipServerpool_jwc_xk']
jsessionid = cookies.values()[1] #获取cookies
login_info_header = {
'Bigipserverpool_Jwc_xk':jwcinfo,
'Sid':'2016210942',
'Jsessionid':jsessionid,
'Authorization':"Basic Base64(2016210942:130395)"
} #但是用这一个header得到的状态码还是502...不知道那里错了...

  • 直接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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    {'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}

requests关于cookie的文档


打印<class 'requests.cookies.RequestsCookieJar'>的key,value:

1
2
3
4
BIGipServerpool_jwc_xk 1028696256.20480.0000
JSESSIONID 320CA9B505D70D76A12D3D826BB6F20E
BIGipServerpool_portal 173058240.20480.0000
JSESSIONID aaa5uKro_uGjW6WSg0MWv

本文地址: http://Humbertzhang.github.io/2017/05/21/模拟登录下获取Cookie/