如何提取 headers.response 值并在下一个请求中使用它?

问题描述 投票:0回答:1

API请求response.headers如下:

Headers([('server', 'nginx'), ('content-type', 'application/json'), ('content-length', '86'), ('content-encoding', 'gzip'), ('bd-tt-error-code', '0'), ('vary', 'Accept-Encoding,Origin'), ('x-tt-logid', '202312170838402529F37099A87D3183C8'), ('x-tt-store-idc', 'useast50'), ('x-tt-store-region', 'us'), ('x-tt-store-sec-uid', 'MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ'), ('tt-idc-switch', '10000@20231215031610'), ('access-control-expose-headers', 'tt-idc-switch'), ('server-timing', 'inner; dur=342'), ('x-tt-trace-host', '018d3bc5ced77196494af1e4061407934b6217a2456ca5e45c0b073ee97f5212289640d2a2b1270fb44af17fcd1630e811046af8087a2cdb115aac237560ac99f7402ce7cc4f5d05098607026e881fdf7a0710ec3d4cb1e8a49b9a5b4a22c714be'), ('x-tt-trace-id', '00-76ed61b410657ea68d8c17862b9504d1-76ed61b410657ea6-01'), ('date', 'Sun, 17 Dec 2023 08:38:40 GMT'), ('x-cache', 'TCP_MISS from server.com (AkamaiGHost/11.3.3-52668873) (-)'), ('set-cookie', 'passport_csrf_token=d5df6670ecf03e53fd2aabba1b0b1bcb; Path=/; Domain=API.com; Max-Age=5184000; Secure; SameSite=None'), ('set-cookie', 'passport_csrf_token_default=d5df6670ecf03e53fd2aabba1b0b1bcb; Path=/; Domain=API.com; Max-Age=5184000'), ('set-cookie', 'reg-store-region=; Path=/; Domain=API.com; Max-Age=0; HttpOnly; Secure'), ('set-cookie', 'store-idc=useast50; Path=/; Domain=API.com; Max-Age=31536000; HttpOnly'), ('set-cookie', 'store-country-code=us; Path=/; Domain=API.com; Max-Age=31536000; HttpOnly'), ('set-cookie', 'store-country-code-src=did; Path=/; Domain=API.com; Max-Age=31536000; HttpOnly'), ('set-cookie', 'tt-target-idc=useast50; Path=/; Domain=API.com; Max-Age=31536000; HttpOnly'), ('set-cookie', 'tt-target-idc-sign=J1Ng0zaiTksaUHc72e3Eak8RycLDh7UkApISS4T6-R4xWqBERvLKb8xTEUGnto-QEGmVu7nbzBYXeScj1_5UBx7DOkiKp96JjDQg6edacTyEF93a28IVU-a9Dp-9tSo5PqlUJTrdPqKwqgmjJCy9T3dZmCuYoh3CeEAdrtMMyzcv9X8RUXDEDx0Vk8UJRpEdPNxfGYWHhh5_Jau-pO75c212wfBDcmucw-Hb6G7ZFoIg0m5sSK2Q6hlSSjsVI7TBWkRybIjZIBHt-OIAux4uZljJNRlyyVxSQiaNAhQ6ChvuclRiQWYtNZHEiz2pBd3pL9SkKX21-4FUHxjBIM1fiY0fki7N7eQWG4b3dja3TnjtE-b9_uQ8l8Q-XH_CYkW1hxpsuVApMwndqdLSmFnBP8UXOq7dF-w_CIizNSd-rQw1aYZToF8ZiddcGoFAt4tXtF_F0mllYdMLpPSGQoNgy2iYYqW1g8FH-p_YmhdxJ0qsMH-Oel0VAd-q0MAbsRsJ; Path=/; Domain=API.com; Max-Age=31536000; HttpOnly'), ('x-tt-trace-tag', 'id=16;cdn-cache=miss;type=dyn'), ('server-timing', 'cdn-cache; desc=MISS, edge; dur=1, origin; dur=360'), ('x-origin-response-time', '360,23.207.216.25'), ('x-check-cacheable', 'NO'), ('x-akamai-request-id', '373e52db')])

我正在尝试提取值“tt-target-idc-sign=”并在下一个请求中使用它。但我无法使用下面的代码获取该值:

response = self.post_request(session, query_str, headers, body)
        print(response.headers)
        for line in response.headers:
            print(line)
            #if line[0] == 'set-cookie':
                #if line[1].startswith('tt-target-idc-sign='):
                    #sign = line[1].split('=', 1)[1].split(';', 1)[0]
                    #print("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", sign)

尝试失败。打印“线”显示如下:

server
content-type
content-length
content-encoding
bd-tt-error-code
vary
x-tt-logid
x-tt-store-idc
x-tt-store-region
x-tt-store-sec-uid
tt-idc-switch
access-control-expose-headers
server-timing
x-tt-trace-host
x-tt-trace-id
date
x-cache
set-cookie
x-tt-trace-tag
x-origin-response-time
x-check-cacheable
x-akamai-request-id

没有“tt-target-idc-sign=”!

那么如何提取值:tt-target-idc-sign=

谢谢你

python api header response-headers
1个回答
0
投票

看不到排队是因为 tt-target-idc-sign 位于 set-cookie 中。你实际上已经提取了价值。只需删除下面您写的注释,您就会看到您的目标数据。

response = self.post_request(session, query_str, headers, body)
    print(response.headers)
    for line in response.headers:
        print(line)
        if line[0] == 'set-cookie':
            if line[1].startswith('tt-target-idc-sign='):
                sign = line[1].split('=', 1)[1].split(';', 1)[0]
                print("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", sign)
print(sign)
© www.soinside.com 2019 - 2024. All rights reserved.