2-004 模拟登陆人人 WANG 2020-04-27

方法一,直接把COOKICES放在HEADERS请求头里面,这里是直接赋值的,

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; [email protected]; 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; [email protected]; 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)

# 请求登陆之后的页面

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)

你可能感兴趣的:(2-004 模拟登陆人人 WANG 2020-04-27)