-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqq_count.py
157 lines (99 loc) · 3.66 KB
/
qq_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
# -*- 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 getGroupNames():
print u"开始.."
result = []
browser.switch_to_window(browser.window_handles[-1])
groupNames = browser.find_elements_by_css_selector(".my-all-group li")
for i in groupNames:
result.append(i.text)
return result
def action(string, groupnames, groupnumbers, last = False):
webElement = browser.find_element_by_css_selector(string)
if(last == False):
print webElement.text
groupnames.append(webElement.text)
ActionChains(browser).double_click(webElement).perform()
indiv = browser.find_element_by_xpath("//div[@class='group-members']")
pattern = re.compile(r'([0-9]+)/[0-9]+')
number = pattern.findall(indiv.text)[0]
groupnumbers.append(number)
time.sleep(SLEEP_TIME)
change = browser.find_element_by_id("changeGroup")
ActionChains(browser).double_click(change).perform()
time.sleep(SLEEP_TIME)
return groupnames, groupnumbers
def getNumbers(groupNames):
groupnames = []
groupnumbers = []
last = ""
for i in groupNames:
string = "li[title=" + "'" + i + "'" + "]"
name = re.sub(' ', ' ', i)
string = "li[title=" + "'" + name + "'" + "]"
last = string
groupnames, groupnumbers = action(string, groupnames, groupnumbers)
groupnames, groupnumbers = action(last, groupnames, groupnumbers, last = True)
return groupnames, groupnumbers
def getTotalNumbers(groupnumbers):
total = 0
for i in groupnumbers:
number = int(i)
total = total + number
return total
def output(groupnames, groupnumbers, totalgroups, totalnumbers):
file = open("log.txt","w")
for i in groupnames:
file.writelines(i)
file.writelines("\n")
file.writelines("\n")
for j in groupnumbers:
file.writelines(j)
file.writelines("\n")
file.writelines("\n")
sum_group = u"总群数:" + str(totalgroups)
sum_peple = u"总人数:" + str(totalnumbers)
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 = "http://qun.qq.com/"
chromedriver = "C:\Program Files\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('')
groupNames = getGroupNames()
groupnames, groupnumbers = getNumbers(groupNames)
groupnames = groupnames[:-1]
groupnumbers = groupnumbers[1:]
totalnumbers = getTotalNumbers(groupnumbers)
totalgroups= len(groupnames)
output(groupnames, groupnumbers, totalgroups, totalnumbers)
print u"总群数:" + str(totalgroups)
print u"总人数:" + str(totalnumbers)
print u"已完成,详见log.txt.."
browser.quit()