两种方式模拟登录人人网,在此记录下
import requests
s = requests.session()
post_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201975959516'
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"}
data = {
"email":"15013122417",
"icode":"",
"origURL":"http://www.renren.com/home",
"domain":"renren.com",
"key_id":"1",
"captcha_type":"web_login",
"password":"2a86f157d37ab25e98f074f7d9f531f4329aeb3120758c313934e31fc2635131",
"rkey":"336daa8bad89c3ac72d7c31ed42dea38",
"f":"https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DZdLxLxabLH2Xt9LgG8PpySNC1BbwIRnjiTas5qDqYfa%26wd%3D%26eqid%3Dfbc691af0031fbef000000035d56098b"
}
s.post(url = post_url,headers=headers,data=data)
get_url = 'http://www.renren.com/971138849/profile'
res = s.get(url = get_url,headers=headers).content.decode()
with open('remrem1.html','w',encoding='utf-8')as fp:
fp.write(res)在这里插入代码片
import urllib.request
import urllib.parse
import http.cookiejar
cj = http.cookiejar.CookieJar()#创建一个cookiejar对象
handler = urllib.request.HTTPCookieProcessor(cj)#通过cookiejar对象创建一个handler
opener = urllib.request.build_opener(handler)#根据handler创建一个opener
post_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201975959516'
# s = requests.session()
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"}
data = {
"email":"15013122417",
"icode":"",
"origURL":"http://www.renren.com/home",
"domain":"renren.com",
"key_id":"1",
"captcha_type":"web_login",
"password":"2a86f157d37ab25e98f074f7d9f531f4329aeb3120758c313934e31fc2635131",
"rkey":"336daa8bad89c3ac72d7c31ed42dea38",
"f":"https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DZdLxLxabLH2Xt9LgG8PpySNC1BbwIRnjiTas5qDqYfa%26wd%3D%26eqid%3Dfbc691af0031fbef000000035d56098b"
}
reque = urllib.request.Request(url = post_url,headers=headers)
formdata = urllib.parse.urlencode(data).encode('utf8')
res = opener.open(reque,data = formdata)
get_url = 'http://www.renren.com/971138849/profile'
# s.post(url,headers = headers,data=data)
request = urllib.request.Request(url = get_url,headers=headers)
response = opener.open(request).read().decode()
with open('remrem.html','w',encoding='utf8') as fp:
fp.write(response)在这里插入代码片
