Python - 使用Cookie获取请求

时间:2018-01-29 12:58:09

标签: python json rest cookies

尽管做了几次尝试,我还是有一个令人费解的要求,我似乎无法将自己挖出来。

背景/工作流程:
1.登录网络服务
2.提出请求
3.获得JSoN

问题:
- 通过浏览器获得的JSON与通过我的代码

获得的JSON不同(读取:有更多数据)

我的代码:

with requests.Session() as s:
        s.headers = {'Accept': 'application/json; indent=4', 'Content-Type': 'application/json'}            
        secret = {
                 "client_id": "xxx",
                 "client_secret": "yyy"
               }
        datas = json.dumps(secret)
        url = 'https://XXXX.yyy/sign_in'
        response = s.post(url, data=json.dumps(secret), headers=s.headers, verify=False)
        print (s.headers)
        print(response)

这部分工作正常,给我一个cookie,我保存在

ck = s.cookies.get_dict()

然后,我需要在成功登录后保存已发送的Cookie请求

url = 'https://give.me/tests'
        print ("Connecting to: " + url)
        # here we do our get, passing the cookies
        response = s.get(url, cookies=ck, verify=False)
        data = response.text
        # print (s.headers) # is OK, returns 200
        # print(data)
        with open('data.txt', 'w') as f:
            f.write(data)
        return 0

预期结果:

"phase": {
"type": "phase",
"activity_level": null,
"logical_name": "phase.test_manual.obsolete",
"name": "Obsolete",
"index": 4,
"id": "phase.test_manual.obsolete"
},

实际结果:

        "phase": {
            "type": "phase",
            "id": "phase.test_manual.obsolete"
        },

文档,以防我在功能上做错了什么:

enter image description here

0 个答案:

没有答案
相关问题