方法一渊胸,直接把COOKICES放在HEADERS請(qǐng)求頭里面统台,這里是直接賦值的粥鞋,
import?requests
header?=?{
????'User-Agent':?'Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/77.0.3865.75?Safari/537.36',
??? 'Cookie':?'anonymid=k2oc3i563739.157310916?5-b85a-c47b678c6576a9;?taihe_bi_sdk_session=71536662d9085ea132e779d1f3bdecc9;?ick=148dfccc-031c-4b09-82df-2ac13756cbf5;?__utma=151146938.373373739.1573109168.1573109270.1578395623.2;?__utmc=151146938;?__utmz=151146938.1578395623.2.2.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/;?first_login_flag=1;?ln_uact=844297347@qq.com;?ln_hurl=http://hdn.xnimg.cn/photos/hdn221/20130723/1740/h_main_w77W_bf650000112f113e.jpg;?jebe_key=af8c32b9-c6ec-4dc2-85d7-f08316801751%7C190f352586cb7e166243b51272b9d865%7C1578395705525%7C1%7C1578395705014;?wp=1;?jebecookies=86f9275f-2134-4759-9923-6ccaa6c9b3ea|||||;?_de=1FAF01AFA458C343C5BC13FF67F7809D696BF75400CE19CC;?p=0156b2612644e97a069aac78e97273b79;?t=587cc526c47870cb39330ab35ec056f09;?societyguester=587cc526c47870cb39330ab35ec056f09;?id=474133869;?xnsid=7fb455c1;?ver=7.0;?loginfrom=null;?wp_fold=0'
}
url?=?'http://www.renren.com/474133869/profile'
r?=?requests.get(url,?headers=header)
with?open('renren3.html',?'w',?encoding='utf-8')?as?f:
????f.write(r.text)
方法二壮韭,放在字典里面,直接是COOKICES的值辑鲤,賦值給COOKICES:
import?requests
header?=?{
????'User-Agent':?'Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/77.0.3865.75?Safari/537.36'
}
cookie?=?'ano57310916gM0sT-9w;?ick_logdae86336a;?taihe_bi_sdk_uid=9fcc8763c918200f1bc47b678c6576a9;?taihe_bi_sdk_session=71536662d9085ea132e779d1f3bdecc9;?ick=148dfccc-031c-4b09-82df-2ac13756cbf5;?__utma=151146938.373373739.1573109168.1573109270.1578395623.2;?__utmc=151146938;?__utmz=151146938.1578395623.2.2.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/;?first_login_flag=1;?ln_uact=844297347@qq.com;?ln_hurl=http://hdn.xnimg.cn/photos/hdn221/20130723/1740/h_main_w77W_bf650000112f113e.jpg;?jebe_key=af8c32b9-c6ec-4dc2-85d7-f08316801751%7C190f352586cb7e166243b51272b9d865%7C1578395705525%7C1%7C1578395705014;?wp=1;?jebecookies=86f9275f-2134-4759-9923-6ccaa6c9b3ea|||||;?_de=1FAF01AFA458C343C5BC13FF67F7809D696BF75400CE19CC;?p=0156b2612644e97a069aac78e97273b79;?t=587cc526c47870cb39330ab35ec056f09;?societyguester=587cc526c47870cb39330ab35ec056f09;?id=474133869;?xnsid=7fb455c1;?ver=7.0;?loginfrom=null;?wp_fold=0'
#?cookie?=?{'anonymid':?'k2oc3i56ypqdrc'}
#?'xnsid:7fb455c1'
cookies?=?{i.split('=')[0]:i.split('=')[1]?for?i?in?cookie.split(';?')}
print(cookies)
url?=?'http://www.renren.com/474133869/profile'
r?=?requests.get(url,?headers=header,?cookies=cookies)
with?open('renren3.html',?'w',?encoding='utf-8')?as?f:
????f.write(r.text)
方法三盔腔,使用SESSION模擬登陸:
import?requests
from?configparser?import?ConfigParser
cfg?=?ConfigParser()
r?=?cfg.read('password.ini')#沖INI里面讀取密碼
pwd?=?cfg.get('password',?'password')
#?print(pwd)
session?=?requests.session()
#?print(session)
post_url?=?'http://www.renren.com/PLogin.do'
post_data?=?{
????'email':?'',
????'password':?pwd
}
header?=?{
????'User-Agent':?'Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/77.0.3865.75?Safari/537.36'
}
#?cookie保存在其中
session.post(post_url,?data=post_data,?headers=header)
#?請(qǐng)求登陸之后的頁(yè)面
response?=?session.get('http://www.renren.com/474133869/profile',?headers=header)
with?open('renren.html',?'w',?encoding='utf-8')?as?f:
????f.write(response.text)