-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.py
125 lines (107 loc) · 4.51 KB
/
User.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
import requests
from lxml import html
import urllib
from requests.packages.urllib3.exceptions import InsecureRequestWarning,InsecurePlatformWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
class User():
def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36',
'Referer':'https://accounts.douban.com/login?alias=&redir=https%3A%2F%2Fwww.douban.com%2F&source=index_nav&error=1001'
}
self.session = requests.Session()
self.session.headers.update(self.headers)
self.url = "https://accounts.douban.com/login"
def getHeader(self):
return self.header
def getCookie(self):
return self.cookie
def doLogin(self,username,password):
cookies = {
'bid': 'vq6Yeh-gYSY',
'__utma': '30149280.1384347154.1511076321.1511076321.1511078922.2',
'__utmc': '30149280',
'__utmz': '30149280.1511076321.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
'push_noty_num': '0',
'push_doumail_num': '0',
'__utmv': '30149280.15544',
'ct': 'y',
'__utmb': '30149280.10.7.1511079546982',
'__utmt': '1',
'ap': '1',
'as': 'https://www.douban.com/group/topic/108719300/?start=0&post=ok',
'ps': 'y',
}
r = self.session.get(self.url)
if r.status_code == 403:
print '登录太频繁,稍后再试试或者换IP地址试试'
exit()
tree = html.fromstring(r.content)
captcha = tree.xpath('//*[@id="captcha_image"]/@src') # 获取验证码图片的链接
captcha_id = tree.xpath('//*[@id="captcha_field"]/../input[2]/@value') # 获取验证码id
print captcha
print captcha_id
data = [
('source', 'group'),
('redir', 'https://www.douban.com'),
('form_email', username),
('form_password', password),
('login', '登陆'),
]
if(len(captcha) > 0):
print captcha[0]
# urllib.urlretrieve(captcha[0],filename="./captcha.png")
ir = self.session.get( captcha[0])
sz = open('cccccccc.jpg', 'wb').write(ir.content)
captcha_value = raw_input('cccccccc.jpg,有验证码请输入:')
solution = ('captcha-solution',captcha_value)
captchaID = ('captcha-id',captcha_id[0])
data.append(solution)
data.append(captchaID)
res = self.session.post('https://accounts.douban.com/login', data=data)
print res
# tree = html.fromstring(_res.content)
# xpath = '//*[@id="content"]/div/div[1]/div[2]/table/tr/td[1]/a/@href';
# urlArr = _tree.xpath(xpath)
# print urlArr
# 重复登录
if res.status_code == 200 :
tow_tree = html.fromstring(r.content)
tow_captcha = tow_tree.xpath('//*[@id="captcha_image"]/@src') # 获取验证码图片的链接
tow_captcha_id = tow_tree.xpath('//*[@id="captcha_field"]/../input[2]/@value') # 获取验证码id
print tow_captcha
print tow_captcha_id
tow_data = [
('source', 'group'),
('redir', 'https://www.douban.com'),
('form_email', username),
('form_password', password),
('login', '登陆'),
]
ir = requests.get(tow_captcha[0])
sz = open('cccccccc.jpg', 'wb').write(ir.content)
captcha_value = raw_input('查看captcha.png,有验证码请输入:')
t_solution = ('captcha-solution', captcha_value)
t_captchaID = ('captcha-id', tow_captcha[0])
tow_data.append(t_solution)
tow_data.append(t_captchaID)
res = requests.post('https://accounts.douban.com/login', cookies=cookies, headers=self.headers, data=tow_data)
# print 'tow--------------------------'
#
# print res
#
# if res.status_code == 200:
# print 'login success cookies is :'
# print res.cookies
#
# print {c.name: c.value for c in res.cookies}
# self.cookie = res.cookies;
# else:
# print 'login error'
#test
#
# u = User();
#
# u.doLogin('15618311747','15618311747')