-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguahao_hos.py
234 lines (212 loc) · 7.37 KB
/
guahao_hos.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
# -*- coding:utf-8 -*-
import random
import socket
import time
import re
import urllib
import urllib2
import json
from openpyxl import Workbook
from openpyxl import load_workbook
socket.setdefaulttimeout(10)
f = open('hospitalId.txt','r')
def get_mid(w1,w2,text) :
pat = re.compile(w1+'(.*?)'+w2,re.S)
result = pat.findall(text)
return result[0]
def get_mid_all(w1,w2,text) :
pat = re.compile(w1+'(.*?)'+w2,re.S)
result = pat.findall(text)
return result
def get_mid_op(w1,w2,text,i) :
pat = re.compile(w1+'(.*?)'+w2,re.S)
result = pat.findall(text)
return result[i]
def clear(s) :
s = s.replace(' ','')
s = s.replace(' ','')
s = s.replace('\n','')
s = s.replace('\r','')
return s
class hospital :
def __init__(self) :
name = 'unknown' #get
location = 'unknown'
phone = '0'
info = 'unknown' #get
comment_num = '0'
yuyue_num = '0' #get
rank = 'unknown' #get
website = 'unknown'
follow = '0'
service = '0'
keshi = []
houzhen_time = '0'
Keshi_string = '无'
hospital_list = []
def get_hos_info(hospital_id) :
global hospital_list
hospital_list.append(hospital())
i = len(hospital_list) - 1
def connect() :
url = 'http://www.guahao.com/hospital/' + str(hospital_id)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read()
def connect_for_info() :
url = 'http://www.guahao.com/hospital/introduction/' + str(hospital_id)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read()
def get_intro() :
try:
text = connect_for_info()
except:
print 'connect to guahao.com fail!'
w1 = '<div class="introduction-content">'
w2 = '</div>'
tmp = get_mid(w1,w2,text)
hospital_list[i].info = clear(tmp)
hospital_list[i].info = hospital_list[i].info.replace('<p>','')
hospital_list[i].info = hospital_list[i].info.replace('</p>','')
hospital_list[i].info = hospital_list[i].info.replace('”','"')
hospital_list[i].info = hospital_list[i].info.replace('“','"')
hospital_list[i].info = hospital_list[i].info.replace('<div>','"')
hospital_list[i].info = hospital_list[i].info.replace('</div>','"')
def get_info():
try:
text = connect()
except:
print 'connect to guahao.com fail'
try:
w1 = '<title>'
w2 = '_微医'
hospital_list[i].name = get_mid(w1,w2,text)
except:
print 'load name fail!'
try:
w1 = '<span>预约量</span>'
w2 = '<span>'
tmp = get_mid(w1,w2,text)
w1 = '<strong>'
w2 = '</strong>'
tmp = get_mid(w1,w2,tmp)
w1 = ' '
w2 = '\n'
hospital_list[i].yuyue_num = get_mid(w1,w2,tmp)
except:
print 'load yuyue_num fail!'
try:
w1 = '<div class="detail word-break">'
w2 = '</h1>'
tmp = get_mid(w1,w2,text)
w1 = '<span>'
w2 = '</span>'
hospital_list[i].rank = clear(get_mid(w1,w2,tmp))
except:
print 'load rank fail!'
try:
w1 = '<span>患者评价</span>'
w2 = '<span>'
tmp = get_mid(w1,w2,text)
tmp = get_mid('<strong>','</strong>',tmp)
hospital_list[i].comment_num = clear(tmp)
except:
print 'load comment_num fail!'
try:
w1 = '<span>候诊时间</span>'
w2 = '</p>'
tmp = get_mid(w1,w2,text)
tmp = clear(get_mid('<strong>','</strong>',tmp))
hospital_list[i].houzhen_time = tmp
except:
print 'load houzhen_time fail!'
try:
w1 = '<span title="'
w2 = '">'
hospital_list[i].location = get_mid(w1,w2,text)
except:
print 'load location fail!'
try:
w1 = '<span>导医服务</span>'
w2 = '</p>'
tmp = get_mid(w1,w2,text)
hospital_list[i].service = clear(get_mid('<strong>','</strong>',tmp))
except:
print 'load service fail!'
try:
w1 = '<b>电话:</b>'
w2 = '</div>'
tmp = get_mid(w1,w2,text)
hospital_list[i].phone = clear(get_mid('<span>','</span>',tmp))
except:
print 'load phone fail!'
try:
w1 = '<b>官网:</b>'
w2 = '</div>'
tmp = get_mid(w1,w2,text)
hospital_list[i].website = get_mid('<span> ','</span>',tmp)
except:
hospital_list[i].website = '无'
try:
w1 = '<span class="mark-count">'
w2 = '</span>'
hospital_list[i].follow = get_mid(w1,w2,text)
except:
print 'load follow fail!'
try:
w1 = 'KSLB'
w2 = '</em>'
hospital_list[i].keshi = get_mid_all(w1,w2,text)
for k in range(len(hospital_list[i].keshi)) :
hospital_list[i].keshi[k] = hospital_list[i].keshi[k].replace('</a>','')
hospital_list[i].keshi[k] = hospital_list[i].keshi[k].replace('<em>','')
hospital_list[i].keshi[k] = hospital_list[i].keshi[k].replace(' ','')
hospital_list[i].keshi[k] = hospital_list[i].keshi[k].replace('''')">''','')
hospital_list[i].keshi[k] = clear(hospital_list[i].keshi[k])
hospital_list[i].Keshi_string = ''
for k in hospital_list[i].keshi :
hospital_list[i].Keshi_string += k
hospital_list[i].Keshi_string += ';'
except:
print 'load keshi fail!'
get_info()
get_intro()
def save_to_xlxs(i,x):
global hospital_list
wb = load_workbook('hospital2.xlsx')
s = wb.active
s.cell(row = i+x,column = 1,value = hospital_list[i-1].name)
s.cell(row = i+x,column = 2,value = hospital_list[i-1].rank)
s.cell(row = i+x,column = 3,value = hospital_list[i-1].location)
s.cell(row = i+x,column = 4,value = hospital_list[i-1].phone)
s.cell(row = i+x,column = 5,value = hospital_list[i-1].website)
s.cell(row = i+x,column = 6,value = hospital_list[i-1].info)
s.cell(row = i+x,column = 7,value = hospital_list[i-1].follow)
s.cell(row = i+x,column = 8,value = hospital_list[i-1].yuyue_num)
s.cell(row = i+x,column = 9,value = hospital_list[i-1].service)
s.cell(row = i+x,column = 10,value = hospital_list[i-1].comment_num)
s.cell(row = i+x,column = 11,value = hospital_list[i-1].houzhen_time)
s.cell(row = i+x,column = 12,value = hospital_list[i-1].Keshi_string)
wb.save('hospital2.xlsx')
def main() :
i = 0
x = input('fitst line:')
y = input('last line:')
for q in range(x-1) :
f.readline()
while 1 :
i += 1
if i+x-1 > y :
break
print 'getting Num.' + str(i+x-1) + ''' hospital's information'''
hospital_id = f.readline().strip()
if not hospital_id :
break
try:
get_hos_info(hospital_id)
save_to_xlxs(i,x)
except:
continue
if __name__ == "__main__" :
main()