如何在POST请求PARSE / GRAB数据后重定向到另一个URL? Python 3.x

时间:2017-09-17 07:40:36

标签: python python-3.x python-requests

我发送一个JSON帖子请求到带有标题的URL和para:

headers = {
    'channel':'mobiApp',
    'Content-Type': 'application/json; charset=UTF-8',
    'Content-Length': '939',
    'Cookie': 'xyzxcasd',
    'User-Agent': 'okhttp/3.8.0'
    }
url = 'https://api.xyz.com/account.jsp'
data = {'act':'login','class':'profile','prftcf':code1,'p':password1,'u':username1,'ver':'0.9.1'}
content = json.dumps(data)
print ("")
print (content)
print ("")
r = requests.post(url,headers=headers,data=content,allow_redirects=True)
z = r.text

(以上是我发送的请求)

我将此作为回应:

{
  "s": 0,
  "err": "",
  "errCode": "",
  "status": "",
  "ccnt": 1,
  "em": "blueyes81382@yahoo.com",
  "pid": "2436999645",
  "bvUserToken": "ee936b7065353389878696fc7cc4d71a646174653d3230313730393137267573657269643d32343336393939363435" 
}

我想要做的是将此帖子请求(如果存在bvUSertoken后)重定向到某个网址,即:https://api.xyz.com/acount/summary,然后打印回复。

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了解决方案

p ['Set-cookie']解析前一个响应头

中的cookie
cookies = (p['Set-Cookie'])

    headers1 = {
        'channel':'mobiApp',
        'Content-Type': 'application/json; charset=UTF-8',
        'Cookie' : cookies,
        'User-Agent': 'okhttp/3.8.0'
        }

url = 'your redirect url'
urlx = url
parsing = requests.get(urlx,headers=headers1)