-
Notifications
You must be signed in to change notification settings - Fork 5
/
wx_count.py
214 lines (146 loc) · 5.16 KB
/
wx_count.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 09 17:16:56 2015
@author: XuGang
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import re
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import os
global browser
SLEEP_TIME = 1
def toTongXunLu():
group = browser.find_element_by_xpath("//div[@class='tab_item no_extra']/a[@class='chat']")
ActionChains(browser).double_click(group).perform()
def getIndivGroup(webElement):
ActionChains(browser).double_click(webElement).perform()
try:
indiv = browser.find_element_by_xpath("//div[@class='title poi']")
except:
time.sleep(SLEEP_TIME)
indiv = browser.find_element_by_xpath("//div[@class='title poi']")
return indiv
def getIndivGroup_button():
try:
button = browser.find_element_by_xpath("//a[@class='button']")
except:
time.sleep(SLEEP_TIME)
button = browser.find_element_by_xpath("//a[@class='button']")
ActionChains(browser).double_click(button).perform()
try:
indiv = browser.find_element_by_xpath("//div[@class='title poi']")
except:
time.sleep(SLEEP_TIME)
indiv = browser.find_element_by_xpath("//div[@class='title poi']")
return indiv
def getFirstGroupNames():
print u"开始.."
time.sleep(3)
groupNames = []
toTongXunLu()
try:
webElement = browser.find_elements(By.XPATH, "//h4[@class='nickname ng-binding']")
except:
time.sleep(SLEEP_TIME)
webElement = browser.find_elements(By.XPATH, "//h4[@class='nickname ng-binding']")
for i in webElement:
groupNames.append(i.text)
return groupNames[0]
def getNumbers(fistGroupName):
result = []
toTongXunLu()
string = "//h4[text() = " + "\'" + fistGroupName + "\'" + "]"
webElement = browser.find_element(By.XPATH, string)
indiv = getIndivGroup(webElement)
temp = (indiv.text).split()[-1]
judge = re.search('^\([0-9]+\)$',temp)
while (not judge):
indiv = getIndivGroup(webElement)
temp = (indiv.text).split()[-1]
judge = re.search('^\([0-9]+\)$',temp)
print indiv.text
result.append(indiv.text)
while(True):
group = browser.find_element_by_xpath("//div[@class='tab_item no_extra']/a[@class='chat']")
ActionChains(browser).double_click(group).perform()
group.send_keys(Keys.DOWN)
indiv = getIndivGroup_button()
temp = (indiv.text).split()[-1]
judge = re.search('^\([0-9]+\)$',temp)
count = 0
while (not judge):
count = count + 1
toTongXunLu()
indiv = getIndivGroup_button()
temp = (indiv.text).split()[-1]
judge = re.search('^\([0-9]+\)$',temp)
if(count == 3):
return result
break
try:
print indiv.text
except:
continue
result.append(indiv.text)
def analysis(result):
total = 0
for i in result:
temp = i.split()[-1]
indiv = int(re.findall('[0-9]+',temp)[0])
total = total + indiv
return total
def output(result, total):
file = open("log.txt","w")
names = []
numbers = []
for i in result:
name = ""
temp_name = i.split()[:-1]
for j in temp_name:
name = name + " " + j
temp_number = i.split()[-1]
number = int(re.findall('[0-9]+',temp_number)[0])
names.append(name)
numbers.append(str(number))
for i in names:
file.writelines(i)
file.writelines("\n")
file.writelines("\n")
for j in numbers:
file.writelines(j)
file.writelines("\n")
file.writelines("\n")
sum_group = u"总群数:" + str(len(result))
sum_peple = u"总人数:" + str(total)
file.writelines(sum_group)
file.writelines("\n")
file.writelines(sum_peple)
file.writelines("\n")
file.close()
return sum_group,sum_peple
if __name__ == '__main__':
url = "https://wx.qq.com"
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
print u"正在打开网页,请稍后.."
print u"请给网速打分,good:1-5:bad"
SLEEP_TIME = raw_input('')
SLEEP_TIME = float(SLEEP_TIME)
browser = webdriver.Chrome(chromedriver)
browser.get(url)
print u"请登录,然后按任意键继续.."
PRICE = raw_input('')
fistGroupName = getFirstGroupNames()
result = getNumbers(fistGroupName)
total = analysis(result)
sum_group,sum_peple = output(result, total)
print u"总群数:" + sum_group
print u"总人数:" + sum_peple
print u"已完成,详见log.txt.."
browser.quit()