-
Notifications
You must be signed in to change notification settings - Fork 10
/
get_query_suggestion_parallel.py
250 lines (220 loc) · 7.29 KB
/
get_query_suggestion_parallel.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
243
244
245
246
247
248
249
250
#encoding=gbk
import multiprocessing as mp
import urllib2
from urllib2 import Request, urlopen, URLError, HTTPError
import json
import sys
import random
import time
import cookielib
import re
import os
reload(sys)
sys.setdefaultencoding( "utf-8" )
def get_random_proxy_ip(proxy_ip):
return random.choice(proxy_ip)
def get_sogou_query_suggestion( index, querys ):
global proxy_ip
global result_path
if not os.path.isdir(result_path+"/suggestion_sogou"):
os.mkdir(result_path+"/suggestion_sogou")
suggestion_filename=result_path+"/suggestion_sogou/"+str(index)
suggestion_file=file(suggestion_filename, "w")
j=0
for i in range(len(querys)):
if i % 10 != index:
continue
if j % 100 == 0:
random_proxy_ip=get_random_proxy_ip(proxy_ip)
print "random_proxy_ip", random_proxy_ip
#添加cookie
__cookie = cookielib.CookieJar()
cookie=urllib2.HTTPCookieProcessor(__cookie)
__req = urllib2.build_opener(cookie)
urllib2.install_opener(__req)
urllib2.urlopen('https://www.sogou.com/')
proxy = urllib2.ProxyHandler({'http': random_proxy_ip })
opener = urllib2.build_opener(proxy)
query=querys[i]
query=query.replace(" ", "%20")
url="https://www.sogou.com/suggnew/ajajjson?key="+ query+"&type=web&ori=yes&pr=web&abtestid=2&ipn="
# print "url", url
#有些词会失败
try:
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'}
request = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(request)
except Exception as e:
print e.reason
j+=1
suggestion_file.write(query+"\t"+"ERROR"+"\n")
continue
html=response.read()
stripped_html=html[17:-5] #去掉头尾无用字符
# print "stripped_html", stripped_html
if stripped_html == "":
print "query, stripped_html", stripped_html, query
j+=1
else:
json_html=json.loads(stripped_html.decode('gbk').encode('utf-8'))
suggestion=""
for suggest in json_html[1]:
if suggestion == "":
suggestion=suggest.decode('utf-8').encode('gbk') #去掉头尾的引号
else:
suggestion+=("\t"+suggest.decode('utf-8').encode('gbk'))
# print "suggestion", suggestion
query=query.replace("%20", " ") #再替换回来
suggestion_file.write(query+"\t"+suggestion+"\n")
j+=1
suggestion_file.close()
#百度也是用utf-8编码
def get_baidu_query_suggestion(index ,querys):
global proxy_ip
if not os.path.isdir(result_path+"/suggestion_baidu"):
os.mkdir(result_path+"/suggestion_baidu")
suggestion_filename=result_path+"/suggestion_baidu/"+str(index)
suggestion_file=file(suggestion_filename, "w")
j=0
for i in range(len(querys)):
if i % 10 != index:
continue
if j % 100 == 0:
random_proxy_ip=get_random_proxy_ip(proxy_ip)
print "random_proxy_ip", random_proxy_ip
#添加cookie
__cookie = cookielib.CookieJar()
cookie=urllib2.HTTPCookieProcessor(__cookie)
__req = urllib2.build_opener(cookie)
urllib2.install_opener(__req)
urllib2.urlopen('http://www.baidu.com/')
proxy = urllib2.ProxyHandler({'http': random_proxy_ip })
opener = urllib2.build_opener(proxy)
# url="http://suggestion.baidu.com/su?wd="+query #
query=querys[i]
query=query.replace(" ", "%20")
url="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd="+query
try:
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'}
request = urllib2.Request(url.decode('gbk').encode('utf-8'), headers=headers)
response = urllib2.urlopen(request)
except Exception as e:
suggestion_file.write(query+"\t"+"ERROR"+"\n")
j+=1
continue
html=response.read()
stripped_html=html[17:-2] #去掉头尾无用字符
if len(stripped_html) == 20:
print "query, stripped_html", stripped_html, query
suggestion_file.write(query+"\n")
j+=1
continue
else:
splitted=stripped_html.split("[") #用[分隔,最后两个字符无用
splitted2=splitted[1][:-2].split(",")
suggestion=""
for k in range(len(splitted2)):
if k == 0:
suggestion=splitted2[k][1:-1] #去掉头尾的引号
else:
suggestion+=("\t"+splitted2[k][1:-1])
query=query.replace("%20", " ") #再替换回来
suggestion_file.write(query+"\t"+suggestion+"\n")
j+=1
suggestion_file.close()
#360也是用utf-8编码
def get_360_query_suggestion(index, querys):
global proxy_ip
if not os.path.isdir(result_path+"/suggestion_360"):
os.mkdir(result_path+"/suggestion_360")
suggestion_filename=result_path+"/suggestion_360/"+str(index)
suggestion_file=file(suggestion_filename, "w")
j=0
for i in range(len(querys)):
if i % 10 != index:
continue
if j % 100 == 0:
random_proxy_ip=get_random_proxy_ip(proxy_ip)
print "random_proxy_ip", random_proxy_ip
#添加cookie
__cookie = cookielib.CookieJar()
cookie=urllib2.HTTPCookieProcessor(__cookie)
__req = urllib2.build_opener(cookie)
urllib2.install_opener(__req)
# urllib2.urlopen('http://www.so.360.cn/')
proxy = urllib2.ProxyHandler({'http': random_proxy_ip })
opener = urllib2.build_opener(proxy)
query=querys[i]
query=query.replace(" ", "%20") #有些引擎无法正确处理带空格的关键词
url="http://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word,obdata&word="+query
try:
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'}
request = urllib2.Request(url.decode('gbk').encode('utf-8'), headers=headers)
response = urllib2.urlopen(request)
except Exception as e:
suggestion_file.write(query+"\t"+"ERROR"+"\n")
j+=1
continue
html=response.read()
stripped_html=html[12:-2] #去掉头尾无用字符
# if stripped_html == "":
# print "query, stripped_html", stripped_html, query
# j+=1
# else:
#把反斜杠进行转义,因为json无法处理反斜杠
regex = re.compile(r'\\(?![/u"])')
fixed_html = regex.sub(r"\\\\", stripped_html)
try:
json_html=json.loads(fixed_html)
except Exception as e:
print "fixed_html", fixed_html
j+=1
continue
suggestion=""
for kv in json_html["result"]:
for key, value in kv.items():
if key=="word":
if suggestion == "":
suggestion=value.decode('utf-8').encode('gbk')
else:
suggestion+=("\t"+value.decode('utf-8').encode('gbk'))
query=query.replace("%20", " ") #再替换回来
suggestion_file.write(query+"\t"+suggestion+"\n")
j+=1
suggestion_file.close()
def read_proxy_ip(proxy_ip):
proxy_ip_file=file(proxy_ip,"r")
proxy_ip_list=[]
for line in proxy_ip_file:
proxy_ip_list.append(line.strip())
return proxy_ip_list
if __name__=='__main__':
proxy_ip=read_proxy_ip("proxy_ip")
engine_name=sys.argv[1]
query_filename=sys.argv[2]
result_path=sys.argv[3]
query_file=file(query_filename, "r")
i=0
querys=[]
for line in query_file:
i+=1
query=line.strip()
querys.append(query)
if i %1000 == 0:
print i
#pool = mul.Pool()
#print mul.cpu_count()
start=time.time()
if engine_name=="sogou":
processes = [mp.Process(target=get_sogou_query_suggestion, args=(i, querys)) for i in range(10) ]
elif engine_name=="baidu":
processes = [mp.Process(target=get_baidu_query_suggestion, args=(i, querys)) for i in range(10) ]
else:
processes = [mp.Process(target=get_360_query_suggestion, args=(i, querys)) for i in range(10) ]
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
end=time.time()
print "it takes %f sec" % (end-start)