-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaidu.py
266 lines (222 loc) · 7.86 KB
/
baidu.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# -*- coding: utf-8 -*-
import re
import csv
import codecs
import urllib2
from bs4 import BeautifulSoup
from pyquery import PyQuery as pq
import time
class BaiduSearchRequest(object):
def post(self, queryStr):
timeout = 5
queryStr = urllib2.quote(queryStr)
url = 'https://www.baidu.com/s?wd=%s' % queryStr
print url
request = urllib2.Request(url)
#伪装HTTP请求
request.add_header('User-agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
request.add_header('connection','keep-alive')
request.add_header('referer', url)
# request.add_header('Accept-Encoding', 'gzip') # gzip可提高传输速率,但占用计算资源
try:
response = urllib2.urlopen(request, timeout = 10)
html = response.read()
except:
print 'not catch'
return None
# response = urllib2.urlopen(request)
return html
# print html
#results = self.extractSearchResults(html)
def anlysis_from_html(self,html_doc):
soup = BeautifulSoup(html_doc, 'html.parser')
li = soup.find('div',class_ = 'nums')
try:
print li.text
except:
return '0'
res = re.findall(r"[0-9]",li.text)
res = "".join(res)
return res
def getResultNum(self,word1):
html = self.post(word1)
while html == None:
html = self.post(word1)
res = self.anlysis_from_html(html)
return int(res)
def main(self,word1,word2):
res1 = self.getResultNum(word1)
res2 = self.getResultNum(word2)
res_both = self.getResultNum(word1+' '+word2)
return res1,res2,res_both
def read_csv(filepath):
llist = []
reader = csv.reader(file(filepath, 'rb'))
for line in reader:
llist.append(line)
return llist
def write_file(filepath,content):
writer = csv.writer(file(filepath, 'a'))
writer.writerow(content)
def get_medical_positive():
baidu = BaiduSearchRequest()
# print baidu.main('电脑','车')
filepath = 'data/syns/medical_syns_7666.txt'
# f = open('data/syns/syns_ALL_1214.txt','r')
f = open(filepath,'r')
count = 0
total = 0
res = []
for line in f:
total += 1
line = line.strip()
# print line
e1 = line.split('|')[0]
e2 = line.split('|')[1]
print e1,e2
res = baidu.main(e1,e2)
with open('data/baidu_7666.csv', mode='a') as f:
f.writelines(e1+'|'+e2+'|%s|%s|%s' % (res[0],res[1],res[2]) + '\n')
f.close()
# write_file('data/baidu_7666.csv',(e1+'|'+e2+'|%s|%s|%s' % (res[0],res[1],res[2])))
def get_nagetive():
baidu = BaiduSearchRequest()
# print baidu.main('电脑','车')
# filepath = 'data/neg_syns/word_from_model_negative_100w.txt'
filepath = 'data/neg_syns/med_negative_100w_De_OOV.txt'
# f = open('data/syns/syns_ALL_1214.txt','r')
f = open(filepath,'r')
count = 0
total = 0
res = []
for line in f:
print total
if total >= 100000:
break
total += 1
if total < 1900:
continue
line = line.strip()
# print line
e1 = line.split('|')[0]
e2 = line.split('|')[1]
print e1,e2
if e1 == '' or e2 == '':
continue
res = baidu.main(e1,e2)
# with open('data/baidu_nagetive_all_model_vocab_1221.csv', mode='a') as f:
with open('data/baidu_nagetive_medical_1221.csv', mode='a') as f:
f.writelines(e1+'|'+e2+'|%s|%s|%s' % (res[0],res[1],res[2]) + '\n')
f.close()
# write_file('data/baidu_7666.csv',(e1+'|'+e2+'|%s|%s|%s' % (res[0],res[1],res[2])))
if __name__ == '__main__':
get_nagetive()
# # baidu.main('小儿休克','婴幼儿休克')
# baidu.main('触诊无肺动脉关闭感','心血管造影见双球征')
# zh_w2v_model = read_in_model(zh_w2v_model_filepath)
# en_w2v_model = read_in_model(en_w2v_model_filepath)
# radios = [1]
# # set_filepath = 'data/%sset_1to%d.csv'
# # features_set_filepath = 'data/%sset_1to%d_svm.csv'
#
# res_file = 'res/baidu_score_1to1_1014.csv'
#
# bd = BaiduSearchRequest()
#
# for radio in radios:
# trainset_filepath = 'data/trainset_1014.csv'#set_filepath % ('train',radio)
# testset_filepath = 'data/testset_1014.csv'#set_filepath % ('test',radio)
#
# # 读入word pair
# trainset = read_csv(trainset_filepath)
# testset = read_csv(testset_filepath)
#
# for i in trainset:
# res1,res2,res_both = bd.main(i[0],i[1])
# line = [i[0],i[1], str(res1) + '|' + str(res2) + '|' + str(res_both)[:-1]]
# # print line
# write_file(res_file,line)
#
# for i in testset:
# res1,res2,res_both = bd.main(i[0],i[1])
# line = [i[0],i[1], str(res1) + '|' + str(res2) + '|' + str(res_both)[:-1]]
# # print line
# write_file(res_file,line)
#
# # features_trainset_filepath = features_set_filepath % ('train',radio)
# # features_testset_filepath = features_set_filepath % ('test',radio)
# #
# # # 保存结果
# # write_file(features_trainset_filepath,trainset_features)
# # write_file(features_testset_filepath,testset_features)
#
# trans.getSymptoms()
# trans.getSymptomsFromCSV('.\data\\merge.csv')
# # -*- coding: utf-8 -*-
#
# import re
# import csv
# import codecs
# import urllib2
# from bs4 import BeautifulSoup
# from pyquery import PyQuery as pq
# import time
# class BaiduSearchRequest(object):
#
# def post(self, queryStr):
# # print url
# try:
# timeout = 5
# queryStr = urllib2.quote(queryStr)
# url = 'https://www.baidu.com/s?wd=%s' % queryStr
# print url
#
# request = urllib2.Request(url)
# #伪装HTTP请求
# request.add_header('User-agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
# request.add_header('connection','keep-alive')
# request.add_header('referer', url)
# # request.add_header('Accept-Encoding', 'gzip') # gzip可提高传输速率,但占用计算资源
# response = urllib2.urlopen(request, timeout = 5)
#
# # response = urllib2.urlopen(request)
# html = response.read()
# return html
# # print html
# #results = self.extractSearchResults(html)
# except Exception as e:
# print 'URL Request Error:', e
# return None
#
# def anlysis_from_html(self,html_doc):
# soup = BeautifulSoup(html_doc, 'html.parser')
# li = soup.find('div',class_ = 'nums')
# print li.text
# res = re.findall(r"[0-9]",li.text)
# res = "".join(res)
# return res
#
# def getResultNum(self,word1):
#
# html = self.post(word1)# + ' ' + word2)
# res = self.anlysis_from_html(html)
# return res
#
# def main(self,word1,word2):
# res1 = self.getResultNum(word1)
# res2 = self.getResultNum(word2)
# res_both = self.getResultNum(word1+' '+word2)
#
# print res1,res2,res_both
#
#
#
#
#
# baidu = BaiduSearchRequest()
# # baidu.main('电脑','车')
# # baidu.main('小儿休克','婴幼儿休克')
# baidu.main('触诊无肺动脉关闭感','心血管造影见双球征')
#
# # trans.getSymptoms()
# # trans.getSymptomsFromCSV('.\data\\merge.csv')