Python爬虫(三)
使用代码通过账号密码登录人人网,并使用登录后的cookie访问人人网主页
源码:
1 import json 2 from urllib import request, parse 3 4 # 导入http中管理cookie的包 5 from http import cookiejar 6 # 创建cookie容器对象,cookie_object 7 cookie_object = cookiejar.CookieJar() 8 # 创建cookie管理器,handler 9 handler = request.HTTPCookieProcessor(cookie_object) 10 # 新建一个带有cookie管理的opener,替换原有的request.urlopen() 11 opener = request.build_opener(handler) 12 13 url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2018721536494' 14 form = { 15 # 登录邮箱/手机/用户名 16 "email": "******", 17 "icode": "", 18 "origURL": "http://www.renren.com/home", 19 "domain": "renren.com", 20 "key_id": "1", 21 "captcha_type": "web_login", 22 "password": "b4462b8c31ac65f01b3089881f3c492d7a8176293e22d582d867a8388bd36115", 23 "rkey": "68ccc6f7c7489ab989ac4d8af82c96d0", 24 "f": "http%3A%2F%2Fzhibo.renren.com%2Ftop", 25 } 26 form_bytes = parse.urlencode(form).encode('utf-8') 27 28 response = opener.open(url, data=form_bytes) 29 html_bytes = response.read() 30 # print(html_bytes) # b'{"code":true,"homeUrl":"http://www.renren.com/home"}' 31 32 res_dict = json.loads(html_bytes.decode('utf-8')) 33 home_url = res_dict['homeUrl'] 34 # 访问主页,带cookie 35 response = opener.open(home_url) 36 html_bytes = response.read() 37 print(html_bytes.decode('utf-8'))