-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoctor_google_search.py
214 lines (159 loc) · 7.41 KB
/
doctor_google_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
import pandas as pd
from datetime import datetime
import time
import os
import random
import requests
from scrapy.http import TextResponse
from pprint import pprint
from datetime import datetime
import time
from twilio.rest import Client
import argparse
HEADERS3 = {
"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",
'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36',
# "Accept-Encoding": "gzip,deflate,sdch",
# "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4,zh-TW;q=0.2",
}
HEADERS1 = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
HEADERS2 = {'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36',}
# def get_doctor_info(DoctorDF):
# doctor_infolist = []
# for _, row in DoctorDF.iterrows():
# # most results: fistname is before lastname
# if row['MiddleName'] == None:
# doctor_info = "{} {} {} {} {} {}".format(row['FirstName'], row['LastName'],
# row['Primaryspecialty'],
# row['City'], row['State'],
# row['Zip.Code'])
# else:
# doctor_info = "{} {} {} {} {} {} {}".format(row['FirstName'], row['MiddleName'], row['LastName'],
# row['Primaryspecialty'],
# row['City'], row['State'],
# row['Zip.Code'])
# # print(doctor_info)
# doctor_infolist.append([row['NPI'], doctor_info])
# return doctor_infolist
def get_doctor_google_result(keyword, pages = 2, timeout = 10):
keyword = keyword.lower()
HEADERS = HEADERS1
searched_urls = []
googleTrendsUrl = 'https://google.com'
r = requests.get(googleTrendsUrl)
if r.status_code == 200:
g_cookies = r.cookies.get_dict()
else:
g_cookies = {}
# first two pages
for page_number in range(pages):
url = "https://www.google.com/search?q=%s&start=%s" % (keyword, (page_number)*10)
# print(url)
# r = requests.get(url, headers=HEADERS, timeout=timeout)
r = requests.get(url, headers=HEADERS, timeout=timeout, cookies = g_cookies)
print(r)
# try:
# r = requests.get(url, headers=HEADERS, timeout=10)
# except:
# r = scraper.get(url, headers=HEADERS, timeout=10)
# idx = 1
# while r.status_code == 429:
# print('Sleep now {}...'.format(idx))
# time.sleep(240*idx)
# HEADERS = HEADERS2
# r = requests.get(url, headers=HEADERS, timeout=timeout, cookies = g_cookies)
# print(r)
# idx += 1
response = TextResponse(r.url, body = r.text, encoding = 'utf-8')
# with open('preview.html', 'w') as f:
# f.write(str(response.body.decode()))
d = response.selector.xpath('.//div[@class="g"]//div[@data-hveid]//a/@href').extract()
# pprint(d)
d = [i for i in d if 'http' in i and 'googleusercontent' not in i and 'translate.google.com' not in i]
print('From ULR {}'.format(url))
print('Get {} results'.format(len(d)))
searched_urls = searched_urls + d
# second = random.randrange(0, 1)
# time.sleep(second)
return searched_urls
if __name__ == '__main__':
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('--angry_flag', type=int, default=3, help=' ')
parser.add_argument('--auth_token', type=str, default='', help='')
parser.add_argument('--account_sid', type=str, default='', help='')
# parser.add_argument('--your_phonenumber', type=str, default=3, help=' ')
args = parser.parse_args()
# db_connection_str = 'mysql+pymysql://root:@localhost:3306/doctorinfo_sample?charset=utf8'
# db_connection = create_engine(db_connection_str)
# df = pd.read_sql('SELECT * FROM physicians_sample', con=db_connection)
auth_token = args.auth_token
account_sid = args.account_sid
if account_sid != '' and auth_token != '':
client = Client(account_sid, auth_token)
else:
client = None
# print('No SMS Client')
start = args.start
end = args.length + start
angry_flag = args.angry_flag
DocListDF_path = args.input_path
Output_path = DocListDF_path.replace('Data', 'Output').replace('.p', '_GoogleSearch_s{}_e{}.p'.format(start, end))
print('Read data from\t{}\nSave results to\t{}\n'.format(DocListDF_path, Output_path))
df = pd.read_pickle(DocListDF_path)
# cols = ['cId', 'crawlFlag', 'NPI', 'LastName', 'FirstName',
# 'MiddleName', 'City', 'State', 'Zip.Code', 'Primaryspecialty']
# DoctorDF = df[cols]
# doctor_infolist = get_doctor_info(DoctorDF)
doctor_infolist = df['keyword'].to_list()
end = len(doctor_infolist) if len(doctor_infolist) < end else end
doctor_infolist = doctor_infolist[start:end]
# save the results to tmp_path
if os.path.exists(Output_path):
GoogleResult = pd.read_pickle(Output_path)
collected_NPIs = GoogleResult['NPI'].to_list()
else:
GoogleResult = pd.DataFrame(columns = ['NPI', 'searched_urls', 'clct_time'])
collected_NPIs = GoogleResult['NPI'].to_list()
print('\n\nCollected NPI {}'.format(len(collected_NPIs)))
flag = 0
angry_flag = int(angry_flag)
# L = []
for idx, doctor_info in enumerate(doctor_infolist):
NPI, keyword = doctor_info
# print(NPI)
if NPI in collected_NPIs:
print('ignore NPI:\t', NPI)
continue
# start searching from here.
print('\n\n{} & {}\t'.format(start + idx, idx) + str(datetime.now()), '\t', keyword, NPI)
if 'student' in keyword.lower():
print('A student here, let him/her go.')
continue
if flag == angry_flag:
print('Stop here: Google is angry!')
break
searched_urls = get_doctor_google_result(keyword, pages = 2)
if len(searched_urls) == 0:
flag += 1
print('No results for:', NPI, keyword, '\tflag is {}/{}\t'.format(flag, angry_flag))
continue
# status = 1
timestamp = str(datetime.now())
d = {
'NPI': NPI,
'searched_urls': searched_urls,
'clct_time': timestamp
}
# L.append(d)
GoogleResult = GoogleResult.append(d, ignore_index=True)
GoogleResult.to_pickle(Output_path)
second = random.randrange(2, 3)
time.sleep(second)
print('Sleep {}s, and Save to {}'.format(second, Output_path))
# print(client)