-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSearch.py
242 lines (225 loc) · 8.24 KB
/
Search.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import Ranking
import pandas as pd
import itchat
def main():
while 1:
print('=' * 20)
print('请输入指令:')
cmd = input()
print('=' * 20)
# -------------------
if cmd == '说明':
# ---------------
print('程序说明\n1.输入"search"进入抓取分支:复制进关键词(以"#"结束,回车分隔)\n2.输入"query"进入多关键词搜索分支\n3.title/domin排名全查询('
'以"title:"/"domain:"开始)\n4.输入"微信登录",扫码后发送"查询结果"给文件传输助手\n5.')
# -------------------
if 'search' in cmd:
# ---多关键词分支
print('- 多关键词抓取分支 -')
print('- 以#结束 -')
keywords = []
# 多行输入关键词
while 1:
k = input()
if k == '#':
print('即将抓取"{k}"...请确认(y/n)'.format(k=keywords))
if input() == 'y':
print('- 开始抓取 -')
Ranking.scrape_multi(keywords)
print('- 抓取完毕 -')
break
else:
break
else:
keywords.append(k)
if cmd == 'query':
print('请选择:1.标题匹配/2.域名匹配')
choose = input()
if choose == '1':
print('- 多关键词标题排名匹配分支 -')
print('- 以#结束 -')
match_title = []
while 1:
k = input()
if k == '#':
print('即将匹配"{k}"...请确认(y/n)'.format(k=match_title))
if input() == 'y':
print('- 开始匹配 -')
frame = pd.read_excel('./data/keyword-multi.xlsx')
match_title_function(frame, match_title)
print('- 匹配完毕 -')
break
else:
break
else:
match_title.append(k)
# -------------------------------
if choose == '2':
print('- 多关键词域名排名匹配分支 -')
print('- 以#结束 -')
match_domain = []
while 1:
k = input()
if k == '#':
print('即将匹配"{k}"...请确认(y/n)'.format(k=match_domain))
if input() == 'y':
print('- 开始匹配 -')
frame = pd.read_excel('./data/keyword-multi.xlsx')
match_domain_function(frame, match_domain)
print('- 匹配完毕 -')
break
else:
break
else:
match_domain.append(k)
# ---------------
if 'title:' in cmd:
key = cmd.replace('title:', '').strip()
try:
frame = pd.read_excel('./data/keyword-multi.xlsx')
Search_Keyword_Multi(frame, key)
except Exception:
print('- 文件不存在 -')
# ------------------------
if 'domain:' in cmd:
key = cmd.replace('domain:', '').strip()
try:
frame = pd.read_excel('./data/keyword-multi.xlsx')
Search_Domain_Multi(frame, key)
except Exception:
print('- 文件不存在 -')
if cmd == '微信登录':
itchat.auto_login(enableCmdQR=2)
itchat.run()
if cmd == 'exit':
break
@itchat.msg_register('Text')
# 自动回复
def text_reply(msg):
if msg['Text'] == '查询结果':
# print('- 按任意键发送查询Results -')
# input()
itchat.send_file('./data/Results.xlsx', 'filehelper')
print('- 文件已发送 -')
elif msg['Text'] == '退出微信':
itchat.logout()
print('- 微信已退出 -')
else:
itchat.send_msg('发送"查询结果"获取Excel结果\n发送"退出微信"退出登录', 'filehelper')
print('发送"查询结果"获取Excel结果\n发送"退出微信"退出登录')
# ----------------匹配函数----------------------------
def match_title_function(frame, match_title):
title_list = []
domain_list = []
rank_list = []
Keyword = []
i = 0
for item in match_title:
tmpframe = frame[frame.keyword == frame.keyword.unique()[i]]
i += 1
for index, row in tmpframe.iterrows():
try:
if item in row.title:
print('关键词 "', item, '"在{k}的排名为:'.format(k=row['keyword']), row['rank'])
Keyword.append(row.keyword)
rank_list.append(row['rank'])
title_list.append(row.title)
domain_list.append(row.domain)
except Exception:
pass
# ------------------------
tmp_dict = {
'Keyword': Keyword,
'rank': rank_list,
'title': title_list,
'domain': domain_list,
}
# -----------------------
tmp_frame = pd.DataFrame(tmp_dict)
tmp_frame.to_excel('./data/Results.xlsx')
print(tmp_frame)
def match_domain_function(frame, match_domain):
title_list = []
domain_list = []
rank_list = []
Keyword = []
i = 0
for item in match_domain:
tmpframe = frame[frame.keyword == frame.keyword.unique()[i]]
i += 1
for index, row in tmpframe.iterrows():
try:
if item in row.domain:
print('域名 "', item, '"在{k}的排名为:'.format(k=row['keyword']), row['rank'])
Keyword.append(row.keyword)
rank_list.append(row['rank'])
title_list.append(row.title)
domain_list.append(row.domain)
except Exception:
pass
# ------------------------
tmp_dict = {
'Keyword': Keyword,
'rank': rank_list,
'title': title_list,
'domain': domain_list,
}
# -----------------------
tmp_frame = pd.DataFrame(tmp_dict)
tmp_frame.to_excel('./data/Results.xlsx')
print(tmp_frame)
# -------------------------------------------
def Search_Keyword_Multi(frame, keyword):
title_list = []
domain_list = []
rank_list = []
Keyword = []
for index, row in frame.iterrows():
try:
if keyword in row.title:
print('关键词 "', keyword, '"在{k}的排名为:'.format(k=row['keyword']), row['rank'])
Keyword.append(row.keyword)
rank_list.append(row['rank'])
title_list.append(row.title)
domain_list.append(row.domain)
except Exception:
pass
# ------------------------
tmp_dict = {
'Keyword': Keyword,
'rank': rank_list,
'title': title_list,
'domain': domain_list,
}
# -----------------------
tmp_frame = pd.DataFrame(tmp_dict)
tmp_frame.to_excel('./data/Results.xlsx')
print(tmp_frame)
def Search_Domain_Multi(frame, word):
title_list = []
domain_list = []
rank_list = []
Keyword = []
for index, row in frame.iterrows():
try:
if word in row['domain']:
print('域名 "', word, '"在{k}的排名为:'.format(k=row['keyword']), row['rank'])
Keyword.append(row['keyword'])
rank_list.append(row['rank'])
title_list.append(row['title'])
domain_list.append(row['domain'])
except Exception:
pass
# ------------------------
tmp_dict = {
'Keyword': Keyword,
'rank': rank_list,
'title': title_list,
'domain': domain_list,
}
# -----------------------
tmp_frame = pd.DataFrame(tmp_dict)
tmp_frame.to_excel('./data/Results.xlsx')
print(tmp_frame)
if __name__ == '__main__':
main()