from **** import get,post #请查看之前文章 Python爬虫入门函数
import json
from urllib import request,parse
from http import cookiejar
# 通过对象保存cookie
cookie_obj = cookiejar.CookieJar()
# handler是一个操作
handler = request.HTTPCookieProcessor(cookie_obj)
# opener 遇到有cookie的response的时候,
# 调用handler内部的一个函数, 存储到cookie object
opener = request.build_opener(handler)
url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018721642732'
form = {
"email": "13522871593",
"icode": "",
"origURL": "http://www.renren.com/home",
"domain": "renren.com",
"key_id": "1",
"captcha_type": "web_login",
"password": "7d9d4460c909c2cb650063724d98817175a1e442066b968fdae7e28486f96d50",
"rkey": "c7ed9a1e2c5b1a7dd7bc76bfc2ca7c5d",
"f": "http%3A%2F%2Fwww.renren.com%2F537479694",
}
#转化为bytes
form_bytes = parse.urlencode(form).encode('utf-8')
response = opener.open(url, form_bytes)
html_bytes = response.read()
#json数据转换为dict
res_dict = json.loads(html_bytes.decode('utf-8'))
home_url = res_dict['homeUrl']
response = opener.open(home_url)
html_bytes = response.read()
with open('renren.html','wb')as f:
f.write(html_bytes)
Python爬虫实现人人网登陆
最新推荐文章于 2023-05-28 23:58:46 发布