-
Notifications
You must be signed in to change notification settings - Fork 1
/
doctor_zocdoc.py
321 lines (251 loc) · 14 KB
/
doctor_zocdoc.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import os
import requests
import pandas as pd
import time
import requests
from scrapy.http import TextResponse
import json
import html
import argparse
import random
from datetime import datetime
from scrapy.http import HtmlResponse
from selenium import webdriver
import time
from datetime import datetime
from urllib.parse import urlencode
headers = {
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36",
}
def get_review(review_selector):
d = {}
d['comment'] = review_selector.xpath('.//*[@itemprop="reviewBody"]//span/text()').extract_first()
d['date'] = review_selector.xpath('.//*[@itemprop="datePublished"]/text()').extract_first()
d['patientName'] = review_selector.xpath('.//*[@itemprop="author"]/text()').extract_first()
d['patient_type'] = review_selector.xpath('.//*[@data-test="review-source"]/text()').extract_first()
d['overallRating'] = review_selector.xpath('.//*[@data-test="stars-svg-wrapper"]/svg/@data-rating').extract_first()
return d
def get_complete_reviews_from_selenium(url, reviewCount, REVIEW_PER_PAGE, webdriver_path):
options = webdriver.ChromeOptions()
# options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
# options.add_argument('--headless')
driver = webdriver.Chrome(webdriver_path, options=options)
driver.get(url)
driver.title
time.sleep(2)
xpath = './/button[@data-test="reviews-read-more-button"]'
for _ in range(int(reviewCount / REVIEW_PER_PAGE )):
more_result_button = driver.find_element_by_xpath(xpath)
print(more_result_button)
driver.execute_script("arguments[0].click();", more_result_button)
time.sleep(1)
response = HtmlResponse(driver.current_url, body=driver.page_source, encoding='utf-8')
reviews = response.xpath('//div[@itemprop="reviews"]')
reviews = [get_review(i) for i in reviews]
# len(reviews)
driver.quit()
return reviews
def get_physician_info_from_zocdoc_url(ph_url, REVIEW_PER_PAGE, webdriver_path, use_webdriver, proxyapi):
doc_info = {}
r = requests.get('http://api.scraperapi.com/', params=urlencode({'api_key': proxyapi, 'url': ph_url}))
# r = requests.get('http://localhost:8050/render.html', params={'url': ph_url, 'wait':0})
response = TextResponse(r.url, body = r.text, encoding = 'utf-8')
xpath = './/script//text()'
json_data = response.xpath(xpath).extract()
# len(json_data)
json_string = [i for i in json_data if 'window.__REDUX_STATE__ = JSON.parse(' in i][0]
json_info = [i for i in json_string.split(';\n') if 'window.__REDUX_STATE__ = JSON.parse(' in i][0]
json_info = json_info.split('JSON.parse(')[1][:-1] # remove prefix
# x = x[:-1] # remove suffix
# json_info
x = json_info
x = json.loads(x)
x = json.loads(x) # yes, double json.load operations
# (1) basic profile
col = 'profile'; profile = x[col]
cols = [ 'providerId', 'isMentalHealthProvider',
'doesProviderHaveInNetworkInsurances',
'commonVirtualVisitReasons', 'customDimensions', 'brandingVanityUrl']
for col in cols: doc_info[col] = profile[col]
# (2) provider
profile_data_provider = profile['data']['provider']
cols = ['isOnlyInNetworkBookable','acceptsNewPatients','errorCode','id','monolithId','status',
'profileUrl','isApproved','prenominal','postnominal','firstName',
'lastName','directoryIds', 'approvedFullName', 'nameInSentence','newPatientAppointmentsAsIntroCalls',
'genderIdentity', 'frontEndCirclePictureUrl', 'frontEndSquarePictureUrl', 'showPhotoWithCircleCss', 'smallPictures',
'averageRating', 'averageBedsideRating','averageWaitTimeRating',
'isPreview','personIdentifierSource', 'dataProvidedBy', 'dataProvidedByUrl',
'mayChargeAdditionalCopay','onlySeesChildren', 'optOutOfRatings', 'previewSummary',
'highlyRecommendPercentage', 'lowWaitTimedPercentage', 'hasNewPatientAvailability',
'isTopRebookingProvider', 'hasVirtualLocations',
'selfPaySettings', 'sellingPoints', 'approvedNonVirtualLocations', 'practices',
'providerLocations', 'relevantSpecialty', 'mainSpecialtyId','specialties','approvedLocations',
'statement', 'defaultProcedureId', 'hospitalAffiliations', 'certifications', 'education',
'memberships', 'typedMemberships','awards','badges', 'languages','npi',
'inNetworkInsurance','procedures','popularProcedures','isBookable','seo','reviewCount',
'reviews', 'properties', 'trustedInsuranceRating', 'providerBadges','locations',
'sortedLocations', 'numVirtualLocations',]
for col in cols: doc_info[col] = profile_data_provider[col]
doc_info['complete_reviews'] = False if doc_info['reviewCount'] > len(doc_info['reviews']) else True
reviewCount = doc_info['reviewCount']
if doc_info['complete_reviews'] == False and use_webdriver == True:
reviews = get_complete_reviews_from_selenium(ph_url, reviewCount,
REVIEW_PER_PAGE, webdriver_path)
print('\nReviews: reported {} vs collected {}'.format(reviewCount, len(reviews)))
if len(reviews) == reviewCount: doc_info['complete_reviews'] = True
doc_info['reviews'] = reviews
return doc_info
if __name__ == '__main__':
REVIEW_PER_PAGE = 50
webdriver_path = '_source/chromedriver_macm1'
parser = argparse.ArgumentParser()
parser.add_argument('--input_path', type = str)
parser.add_argument('--start', type=int, default=0, help=' ')
parser.add_argument('--length', type=int, default=10000, help=' ')
parser.add_argument('--chunk', type=int, default=500, help=' ')
# parser.add_argument('--provider', type=str, default='500', help=' ')
# parser.add_argument('--api_key', type=str, default='500', help=' ')
parser.add_argument('--webdriver', type=int, default=0, help=' ')
parser.add_argument('--angry', type=int, default=10, help=' ')
parser.add_argument('--proxyapi', type=str, default='None', help=' ')
args = parser.parse_args()
angry_flag = args.angry
proxyapi = args.proxyapi
# provider, api_key
use_webdriver = True if args.webdriver == 1 else False
# print(use_webdriver, type(use_webdriver))
# provider = args.provider
# api_key = args.api_key
start = args.start
end = args.length + start
input_path = args.input_path
df = pd.read_pickle(input_path)
name = 'zocdoc'
url_list = df[-df[name].isna()][name].to_list()
source_npi_list = df[-df[name].isna()]['NPI'].to_list()
# print(df.shape)
# print(len(url_list))
end = len(url_list) if len(url_list) < end else end
url_list = url_list[start:end]
source_npi_list = source_npi_list[start:end]
assert len(url_list) == len(source_npi_list)
OutputFolder = input_path.replace('.p', '_s{}_e{}'.format(start, end)).replace('Data', 'Output')
Error_Output_path = os.path.join(OutputFolder, name + '_errorlog.txt') # Output_path.replace('.p', '_errorlog.txt')
if not os.path.exists(OutputFolder):
os.makedirs(OutputFolder)
# print('Read data from\t{}\nSave results to\t{}\n'.format(input_path, Output_path))
print('Read doctor list from: \t{}\nSave results to:\t{}\nSave Error Log to:\t{}'.format(input_path, OutputFolder, Error_Output_path))
# save the results to tmp_path
pkl_files = [os.path.join(OutputFolder, i) for i in os.listdir(OutputFolder) if '.p' in i]
for file in pkl_files:
print('\n' + file )
chunk = int(args.chunk)
## Loop the doctors
error_list = []
min_sec = 1
angry_events = 0
old_idx = 0
total_sec = 0
succ_url = 0
total_start_time = datetime.now()
for idx, urls in enumerate(url_list):
# current url's chunk_id
chunk_id = int(idx / chunk)
new_s = start + chunk_id*chunk
new_e = start + (chunk_id+1)*chunk if start + (chunk_id+1)*chunk < end else end
chunk_name = '{}_s{}_e{}.p'.format(name, new_s, new_e)
chunk_file = os.path.join(OutputFolder, chunk_name)
source_npi = source_npi_list[idx]
# generate Results
if idx % chunk == 0:
print('\n\nChunk {}: Generate the new Result for the new Chunk...'.format(chunk_id))
if os.path.isfile(chunk_file):
Result = pd.read_pickle(chunk_file)
else:
cols = ['providerId', 'isMentalHealthProvider', 'doesProviderHaveInNetworkInsurances', 'commonVirtualVisitReasons',
'customDimensions', 'brandingVanityUrl', 'isOnlyInNetworkBookable', 'acceptsNewPatients',
'errorCode', 'id', 'monolithId', 'status', 'profileUrl', 'isApproved', 'prenominal',
'postnominal', 'firstName', 'lastName', 'directoryIds', 'approvedFullName',
'nameInSentence', 'newPatientAppointmentsAsIntroCalls', 'genderIdentity',
'frontEndCirclePictureUrl', 'frontEndSquarePictureUrl', 'showPhotoWithCircleCss',
'smallPictures', 'averageRating', 'averageBedsideRating', 'averageWaitTimeRating',
'isPreview', 'personIdentifierSource', 'dataProvidedBy', 'dataProvidedByUrl', 'mayChargeAdditionalCopay',
'onlySeesChildren', 'optOutOfRatings', 'previewSummary', 'highlyRecommendPercentage',
'lowWaitTimedPercentage', 'hasNewPatientAvailability', 'isTopRebookingProvider', 'hasVirtualLocations',
'selfPaySettings', 'sellingPoints', 'approvedNonVirtualLocations', 'practices', 'providerLocations',
'relevantSpecialty', 'mainSpecialtyId', 'specialties', 'approvedLocations', 'statement', 'defaultProcedureId',
'hospitalAffiliations', 'certifications', 'education', 'memberships', 'typedMemberships', 'awards', 'badges',
'languages', 'npi', 'inNetworkInsurance', 'procedures', 'popularProcedures', 'isBookable', 'seo',
'reviewCount', 'reviews', 'properties', 'trustedInsuranceRating', 'providerBadges', 'locations',
'sortedLocations', 'numVirtualLocations', 'complete_reviews',
'url', 'clct_time', 'source_npi']
Result = pd.DataFrame(columns = cols)
# Result.to_pickle(chunk_file)
# we have a Result now by any cases.
for url in urls:
# case 1
if url in Result['url'].values:
# if url not in Result['url'].values:
# print('url is not in collected_NPIs', url)
# print(chunk_file)
# assert url in Result['url'].values
print('pass URL: {}'.format(url))
continue
# case 2
s = datetime.now()
try:
print('\n\nidx {} & {}: '.format(start + idx, idx) + url)
doc_info = get_physician_info_from_zocdoc_url(url, REVIEW_PER_PAGE, webdriver_path, use_webdriver, proxyapi)
print('doctor name is: {}'.format(doc_info['approvedFullName']))
except Exception as e:
print('Encounter the error {}. \nGo to next one...'.format(str(e)))
error_list.append({'idx':idx, 'url':url, 'error': str(e), 'time': str(datetime.now())})
pd.DataFrame(error_list).to_csv(Error_Output_path)
if old_idx == idx - 1:
angry_events += 1
else:
angry_events = 1
# update old_idx
print('Last Error occur at idx:', old_idx)
old_idx = idx
if angry_events >= angry_flag:
raise(ValueError('Stop here, Zocdoc is angry!'))
continue
doc_info['url'] = url
doc_info['clct_time'] = datetime.now()
doc_info['source_npi'] = source_npi
print('Reviews: reported {} vs collected {}'.format(doc_info['reviewCount'], len(doc_info['reviews'])))
Result2 = Result.append(doc_info, ignore_index=True)
Result2.to_pickle(chunk_file.replace('.', '_tmp.'))
Result2 = pd.read_pickle(chunk_file.replace('.', '_tmp.'))
os.remove(chunk_file.replace('.', '_tmp.'))
try:
Result2 = Result.append(doc_info, ignore_index=True)
Result2.to_pickle(chunk_file.replace('.', '_tmp.'))
Result2 = pd.read_pickle(chunk_file.replace('.', '_tmp.'))
os.remove(chunk_file.replace('.', '_tmp.'))
except:
# case 2.e2
print('Writing Errors {}. \nGo to next one...'.format(str(e)))
error_list.append({'idx':idx, 'url':url, 'error': 'FileIOError:'+str(e), 'time': str(datetime.now())})
pd.DataFrame(error_list).to_csv(Error_Output_path)
continue
Result = Result.append(doc_info, ignore_index=True)
Result.to_pickle(chunk_file)
print('Save data to: {}'.format(chunk_file))
second = random.randrange(3, 6)
time.sleep(second)
e = datetime.now()
current_time_usage = e - s
total_sec += current_time_usage.total_seconds()
succ_url +=1
current_time = datetime.now()
total_time_used = current_time - total_start_time
print('CurrentTime: {}; Time Used: {}; Succ URL: {}; Avg Time: {}; Total Time {}'.format(current_time, current_time_usage, succ_url, round(total_sec/succ_url, 4), total_time_used) )