狂甩酷拽吊炸天
分类: Python/Ruby
2017-05-03 11:27:55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#! /usr/bin/env python
#coding:utf-8
import sys
import re
import urllib2
import urllib
import requests
import cookielib
## 这段代码是用于解决中文报错的问题
reload(sys)
sys.setdefaultencoding("utf8")
#####################################################
#登录人人
loginurl = ''
logindomain = 'renren.com'
class Login(object):
def __init__(self):
self.name = ''
self.passwprd = ''
self.domain = ''
self.cj = cookielib.LWPCookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
def setLoginInfo(self,username,password,domain):
'''设置用户登录信息'''
self.name = username
self.pwd = password
self.domain = domain
def login(self):
'''登录网站'''
loginparams = {'domain':self.domain,'email':self.name, 'password':self.pwd}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'}
req = urllib2.Request(loginurl, urllib.urlencode(loginparams),headers=headers)
response = urllib2.urlopen(req)
self.operate = self.opener.open(req)
thePage = response.read()
if __name__ == '__main__':
userlogin = Login()
username = 'username'
password = 'password'
domain = logindomain
userlogin.setLoginInfo(username,password,domain)
userlogin.login()
|