python模拟登录,获得cookies,携带cookies

python代码:

requests请求:

import requests

# 模拟登陆的入口
url = 'https://passport.17k.com/ck/user/login'
# 携带请求参数
data = {
    'loginName': '你的账号',
    'password': '你的密码',
}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
res = requests.post(url, data=data, headers=headers)
print(res)
cookies = dict(res.cookies)
# 登陆后才能访问的
url = 'https://user.17k.com/ck/user/myInfo/96139098?bindInfo=1&appKey=2406394919'
res = requests.get(url, headers=headers, cookies=cookies)
print(res)
print(res.text)

session请求:

import requests

# 模拟登陆的入口
url = 'https://passport.17k.com/ck/user/login'
# 携带请求参数
data = {
    'loginName': '你的账号',
    'password': '你的密码',
}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
session = requests.session()
session.post(url, data=data, headers=headers)
# 登陆后才能访问的
url = 'https://user.17k.com/ck/user/myInfo/96139098?bindInfo=1&appKey=2406394919'
res = session.get(url, headers=headers)
print(res)
print(res.text)

 

你可能感兴趣的:(案例,python,python,开发语言,爬虫)