【重磅】Cookies规律总结,搞定卡点
登录后开始正式获取数据阶段:
不使用session:
放在请求头headers中
放在请求体body中:
GET请求
使用session:
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)