【重磅】Cookies、headers、Session规律总结,搞定卡点

【重磅】Cookies规律总结,搞定卡点

登录后开始正式获取数据阶段:

  • 不使用session:

    • 放在请求头headers中

      • 当如是:headers = {“user-agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36”,“Cookie”: “cookie_name1=cookie_value1; cookie_name2=cookie_value2”},将请求头上的复制下来即可
      • 开个脑洞:
        • 提取session中的cookies来适配请求头headers的格式:
          • 获取 Session 携带的 Cookie: cookies = session.cookies
          • 将Cookie设置到Headers中:headers = {‘Cookie’: '; '.join([f"{cookie.name}={cookie.value}" for cookie in cookies])}
          • 然后发送带有Cookie的Headers的请求即可
          • “;”.join()方法:将一个列表中的元素用特定的分隔符连接成一个字符串时
          • eg:
            • my_list = [‘apple’, ‘banana’, ‘orange’]
            • 使用分号和空格连接列表中的元素: result = '; '.join(my_list)
            • print(result): apple; banana; orange
    • 放在请求体body中:

      • cookies = { “cookie_name1”: “cookie_value1”, “cookie_name2”: “cookie_value2”,“cookie_name3”: “cookie_value3” }
      • 提取session中的cookies来适配请求体body的格式
        • 获取Session携带的 Cookie:cookies = session.cookies
        • 将Cookie 转换为字典: dict_cookie = requests.utils.dict_from_cookiejar(cookies)
        • 打印字典形式的 Cookie: print(dict_cookie)
      • 拓展:将字典形式的 Cookie 转换为 RequestsCookieJar对象
        • 获取Session携带的 Cookie:cookies = session.cookies
        • 将Cookie 转换为字典: dict_cookie = requests.utils.dict_from_cookiejar(cookies)
        • 将字典形式的Cookie转换为RequestsCookieJar对象:headers_cookiejar = requests.utils.cookiejar_from_dict(dict_cookie)
        • 当然这一步暂时没发现实用价值,大概只要知道居然还能这样转换即可
  • GET请求

    • 原始状态,形如url?keyname1=value1&keyname2=value2……
    • 拆解成: url ,params字典来盛放参数
      • 注意:API 规定了重复参数名的方式来表示多个日期范围,那么您需要按照规定的方式传递参数。在这种情况下,服务器应该有相应的逻辑来解析和处理这样的参数。
      • eg: “orderTimeBetween”: [“2023-10-30 00:00:00”,“2023-10-31 23:59:59”]
      • response = requests.get(url, params=params,headers=headers)
  • 使用session:

    • 1.创建Session对象: session = request.Session()
    • 2.发送GET/POST请求: session.post()/session.get()
    • 3.获取Session携带的 Cookie:cookies = session.cookies
CookieJar = session.cookies
dict_cookie = requests.utils.dict_from_cookiejar(CookieJar)

# 登录后
url1= "https://imadmin.acetopbms.com/Reward/List"

params = {
    "Include": "SUM(CostPoint)totalPoint",
    "Skip": "0",
    "Take": "2000",
    "channel": "igoldhk",
    "orderBy": "orderTime",
    "orderTimeBetween":  [
        "2023-10-30 00:00:00",
        "2023-10-31 23:59:59"
    ]

}

# 日期请求参数一样
url2 = 'https://imadmin.acetopbms.com/Reward/List?Include=SUM(CostPoint)totalPoint&Skip=0&Take=2000&channel=igoldhk&orderByDesc=orderTime&orderTimeBetween=2023-10-30+00:00:00&orderTimeBetween=2023-10-31+23:59:59'

# 后台复制来的,容易失效 

cookie = '填写自己的!!!'

CookieJar = session.cookies

# headers中放cookie
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
    "Cookie": '; '.join([f"{cookie.name}={cookie.value}" for cookie in CookieJar])
}

# 注:在 HTTP 请求的 Headers 中,Cookie 是大小写不敏感的标头字段,但通常约定使用首字母大写的形式 "Cookie"。根据 HTTP 规范,标头字段名称是大小写不敏感的,但推荐使用首字母大写的形式,以提高可读性和遵循惯例。

# headers中不放cookie

headers2 = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
}

# 方式1:params 
pd.DataFrame(requests.get(url=url1,headers=headers,params =params ).json()["results"]).sort_values(by='orderTime',ascending=True)[['nickName','userNickName','prizeAmount','costPoint','orderTime']]


# 方式2:
pd.DataFrame(requests.get(url=url2,headers=headers).json()["results"]).sort_values(by='orderTime',ascending=True)[['nickName','userNickName','prizeAmount','costPoint','orderTime']]

# 方式3: cookie放入请求体中
pd.DataFrame(requests.get(url=url1,headers=headers2,cookies= dict_cookie,params =params ).json()["results"]).sort_values(by='orderTime',ascending=True)[['nickName','userNickName','prizeAmount','costPoint','orderTime']]

# url2,无params
pd.DataFrame(requests.get(url=url2,headers=headers2,cookies =dict_cookie).json()["results"]).sort_values(by='orderTime',ascending=True)[['nickName','userNickName','prizeAmount','costPoint','orderTime']]

重磅:相互转换

# ①session.cookies ==> ②请求头cookie ==> ③请求体cookie

# ①->②:

# 获取 Session 携带的 Cookie
CookieJar  = session.cookies

# 将 Cookie 设置到 Headers 中
headers = {'Cookie': '; '.join([f"{cookie.name}={cookie.value}" for cookie in CookieJar])}

# 发送带有 Headers 中的 Cookie 的请求
response = requests.get('https://example.com', headers=headers)

# ------------------------------------------------------------------------
# ①->③:

# 获取 Session 携带的 Cookie
CookieJar = session.cookies

# 将 Cookie 转换为字典
dict_cookie = requests.utils.dict_from_cookiejar(CookieJar )

# print(dict_cookie)

# 将Cookie放入请求体中:
requests.get(url=url1,params=params,headers=headers2,cookies =dict_cookie)

# ------------------------------------------------------------------------

# ②->③:

def convert_cookie_to_body(cookie_header):
    cookies = {}
    cookie_parts = cookie_header.split(';')
    for part in cookie_parts:
        key_value = part.strip().split('=')
        if len(key_value) == 2:
            key = key_value[0].strip()
            value = key_value[1].strip()
            cookies[key] = value
    return cookies


# 获取 Session 携带的 Cookie
CookieJar = session.cookies

# 将请求头中的 Cookie 转换为请求体中的 Cookie

cookie_header = '; '.join([f"{cookie.name}={cookie.value}" for cookie in CookieJar]) 
cookie_header  = headers['Cookie']

dict_cookies1 = convert_cookie_to_body(cookie_header)
print(dict_cookies1)
# ------------------------------------------------------------------------
# ③->①:
headers_cookiejar = requests.utils.cookiejar_from_dict(dict_cookies1 )
headers_cookiejar

# 打印 Cookie
for cookie in CookieJar:
    print(cookie.name, cookie.value)

你可能感兴趣的:(python)