方法1:直接登陆对应网址后,F12网页检查,在network中选中任意一网址,复制cookies直接登陆
import requests
def douban_login():
url = 'https://www.douban.com/people/221725636/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (K。。。。。。。。。。。。。。。。。。。。。。4492.400',
# 如果不带,无法进去个人主页
'Cookie': 'bid=CoFnyYaHvxQ; douban-fav-remind=1; __yadk_uid=MGLPA6D5bDkj8JypPNYfwDtVVqUUC1nu; ll="118281"; push_doumail_num=0; __utmv=30149280.22172; gr_user_id=59b923b6-172f-4e68-a7f1-4a2facfb25c7; _ga=GA1.1.843993488.1628860359; _ga_RXNMP372GL=GS1.1.1628860359.1.0.1628860362.0; push_noty_num=0; __gads=ID=a6e23bf0b365acc8-22f68e9c26cb0。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。6343244.1631934791.1631946579.12; __utmz=30149280.1631946579.12.11.utmcsr=movie.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/typerank; __utmt=1; dbcl2="221725636:8nlW+/9/z7g"; ck=dF24; ap_v=0,6.0; _pk_id.100001.8cb4=ee514d0cfc78c7ff.1626343189.12.1631946979.1631437922.; __utmb=30149280.6.10.1631946579'
}
# 发送请求
res = requests.get(url,headers=headers).content.decode()
# 保存文件
with open('第三天/douban_login5.html','w',encoding='utf-8') as f:
f.write(res)
douban_login()
第二种:在发送的时候以传参的方式发送cookie
import requests
def douban_login():
url = 'https://www.douban.com/people/221725636/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537。。。。。。。。。。wser/10.8.4492.400'
}
# 发送请求
# 在发送的时候以传参的方式发送cookie
cookies = 'bid=CoFnyYaHvxQ; douban-fav-remind=1; __yadk_uid=MGLPA6D5bDkj8JypPNYfwDtVVqUUC1nu; ll="118281"; push_doumail_num=0; __utmv=30149280.22172; gr_user_id=59b923b6-172f-4e68-a7f1-4a2facfb25c7; _ga=GA1.1.843993488.1628860359; _ga_RXNMP372GL=GS1.1.1628860359.1.0.1628860362.0; push_noty_num=0; __gads=ID=a6e23bf0b365acc8-22f68e9c26。。。。。。。。。。。。。。。。。2.11.utmcsr=movie.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/typerank; __utmt=1; dbcl2="221725636:8nlW+/9/z7g"; ck=dF24; ap_v=0,6.0; _pk_id.100001.8cb4=ee514d0cfc78c7ff.1626343189.12.1631946979.1631437922.; __utmb=30149280.6.10.1631946579'
# 字典化
cookies_dict = {cook.split('=')[0]:cook.split('=')[1] for cook in cookies.split('; ')}
res = requests.get(url,headers=headers,cookies=cookies_dict).content.decode()
# 保存文件
with open('第三天/douban_login4.html','w',encoding='utf-8') as f:
f.write(res)
douban_login()
# 测试一下传参的方法
# cookies = 'bid=CoFnyYaHvxQ; douban-fav-remind=1; __yadk_uid=MGLPA6D5bDkj8JypPNYfwDtVVqUUC1nu; ll="118281"; push_doumail_num=0; __utmv=30149280.22172; gr_user_id=59b923b6-172f-4e68-a7f1-4a2facfb25c7; _ga=GA1.1.843993488.1628860359; _ga_RXNMP372GL=GS1.1.1628860359.1.0.1628860362.0; push_noty_num=0; __gads=ID=a6e23bf0b365acc8-22f68e9c26cb00ec:T=1626343190:RT=1630057951:S=ALNI_MZMlGap_PH8_YLi1pqpXy-wQMkdqA; __utmc=30149280; _pk_ref.100001.8。。。。。。。。。。。。。。6343244.1631934791.1631946579.12; __utmz=30149280.1631946579.12.11.utmcsr=movie.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/typerank; __utmt=1; dbcl2="221725636:8nlW+/9/z7g"; ck=dF24; ap_v=0,6.0; _pk_id.100001.8cb4=ee514d0cfc78c7ff.1626343189.12.1631946979.1631437922.; __utmb=30149280.6.10.1631946579'
# # 字典化
# cookies_dict = {cook.split('=')[0]:cook.split('=')[1] for cook in cookies.split('; ')}
# for cook in cookies.split('; '):
# print({cook.split('=')[0]:cook.split('=')[1]})
# print(cookies_dict)