选择登录社交网站得越过验证码,所以选没验证码的界面,先看下不用验证码的链接
第一步,先查看下51是怎么登录的:
![](https://app.yinxiang.com/shard/s19/res/5d334e48-5066-44eb-9f73-29e85a871ec9.png?resizeSmall&width=1100)
那就转入http://passport.51.com/login.5p?passport_51_user=YOURUSERNAME?passport_51_password=YOURPASSWORD
发现可以登录,登录后跳转的页面是http://my.51.com/?from=passport
那么就可以开始写程序了:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2, urllib, cookielib, re
class LoginRenren(object):
"""登陆51构造器"""
def __init__(self, USER, password):
self.USER = USER
self.password = password
#self.domain = 'renren.com'
self.domain = '51.com'
self.logined_url = ''
self.logined_url_re = r'http://my.51.com/?from=passport'
self.islogin = False
try:
# 获取构建cookie
cookie = cookielib.CookieJar()
cookieProc = urllib2.HTTPCookieProcessor(cookie)
except:
raise
else:
#装载cookies
opener = urllib2.build_opener(cookieProc)
urllib2.install_opener(opener)
def login(self):
"""登陆方法"""
print 'login...'
# 登陆地址
url='http://passport.51.com/login.5p'
# post数据
postdata = {
'passport_51_user': self.USER,
'passport_51_password': self.password,
}
# 编码登陆的post数据
data = urllib.urlencode(postdata)
#print url, data
# 构造请求
req = urllib2.Request(
url,
data
)
# 发送请求,获取响应
response = urllib2.urlopen(req)
# 登陆后的url
self.logined_url = response.geturl()
# 判断登陆是否成功
self.__isLogin()
if self.islogin == True:
print u'登陆成功 !'
self.__getStatus()
else:
print u'登录失败,请重试 !'
def __isLogin(self):
"""判断登陆是否成功"""
if self.logined_url_re == self.logined_url:
self.islogin = True
def __getStatus(self):
pass
if __name__ == '__main__':
# 请输入您的用户名
USER = raw_input(u'user:')
# 请输入您的密码
PASSWORD = raw_input(u'password:')
lr = LoginRenren(USER, PASSWORD)
lr.login()