-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapePharms_selenium_chrome.py
286 lines (222 loc) · 8.78 KB
/
ScrapePharms_selenium_chrome.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
#Tacrolimus levels: SA03452243 (UCT Private)
#Urine drugs of abuse: SA03450896 (2 Military Hospital lab)
import requests, bs4
from bs4 import BeautifulSoup
from lxml import html
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
driver = webdriver.Chrome()
#If you do not know how to use Selenium, here is a quick overview:
driver.get("http://10.184.55.233:8080") #Browser goes to pharms.westerncape.gov.ac.za
#Finding elements: Use either the ELEMENTS or ELEMENT method. Examples:
#driver.find_element_by_css_selector("#IWEDITUSERNAME") #Find your country in Google. (singu
#sitename = driver.find_element_by_css_selector("#IWLABELLOGINDESCRIPTION")
#print(sitename.text)
assert "Pharmacology" in driver.title
#COMPLETE THE USERNAME AND PASSWORD FIELDS
#Find the username field by its id in the HTML markup (e.g. id="uid) and the password by the name attribute (e.g. name="pwd")
username = driver.find_element_by_id("IWEDITUSERNAME")
username.clear()
username.send_keys("WARD")
password = driver.find_element_by_id("IWEDITPASSWORD")
password.clear()
password.send_keys("WARD")
#CLICK THE LOGIN BUTTON
#Now we need to submit the login credentials by clicking the submit button
driver.find_element_by_id("IWBUTTONLOGIN").click()
#CLICK A LINK ON THE PAGE BASED ON THE LINK TEXT
#This is handy where you know the text of the link you want to target, but there's no unique identifier reliably grip onto in the mark up. Here, we're simply looking for a link with the text: "Grumpy cats".
##### driver.find_element_by_link_text("Grumpy cats").click()
'''
#To log into the homepage:
#import requests
# Fill in your details here to be posted to the login form.
headers = {
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
'Origin': 'http://10.184.55.233:8080',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://10.184.55.233:8080',
'Accept': 'text/html, */*; q=0.01',
}
payload = {
'IWEDITUSERNAME': 'WARD^',
'IWEDITPASSWORD': 'WARD^',
}
# The following is the older requests scrape code
"""
# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
p = s.post('http://10.184.55.233:8080', headers=headers, data=payload, verify=False)
# print the html returned or something more intelligent to see if it's a successful login page.
soup = BeautifulSoup(p.text, 'lxml')
print(soup.prettify())
# An authorised request.
r = s.get('http://10.184.55.233:8080/$')
#print(r.BeautifulSoup(r.text, 'lxml')
#print(soup.get_text)
#print(soup.prettify())
cookies = {
'IW_CookieCheck_': 'Enabled',
'IW_CustomTrackID': '0cse19b15u30qf1bkqfu017cta6q',
'IntraWeb_WWDisa': '0cse19b15u30qf1bkqfu017cta6q_',
}
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Origin': 'http://10.184.55.233:8080',
'Upgrade-Insecure-Requests': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36 OPR/64.0.3417.61',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://10.184.55.233:8080/$',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9',
}
data = {
'IWLISTBOXPERIOD': '0^',
'IWLISTBOXWARD': '0^',
'IWLISTBOXLOCATION': '3^',
'Name3': '^',
'name1': 'IWRadioButtonExact^',
'Name2': '^',
'IWEDITFILENO': '^',
'IWEDITNAME': '^',
'IWEDITINITS': '^',
'IWEDITAGE': '^',
'IWEDITSEX': '^',
'IWEDITDOB': '^',
'IWEDITID': 'SA03452243^',
'IWEDITTESTS': '^',
'IWBUTTONSEARCH': '^',
'IWBUTTONRESET': '^',
'IWGRIDMYPATIENTS': '^',
'IWEDIT1': '^',
'IW_Action': 'IWLISTBOXLOCATION^',
'IW_ActionParam': '^',
'IW_FormName': 'MyPatientsForm^',
'IW_FormClass': 'TMyPatientsForm^',
'IW_width': '843^',
'IW_height': '757^',
'IW_TrackID_': '13'
}
response = requests.post('http://10.184.55.233:8080/$', headers=headers, cookies=cookies, data=data, verify=False)
soup = response.text
webpagehtml = bs4.BeautifulSoup(response.text)
print(webpagehtml.prettify())
########## This is the curl when logging in #################
import requests
cookies = {
'IW_CookieCheck_': 'Enabled',
'IW_CustomTrackID': '0cse19b15u30qf1bkqfu017cta6q',
'IntraWeb_WWDisa': '0cse19b15u30qf1bkqfu017cta6q_',
}
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Origin': 'http://10.184.55.233:8080',
'Upgrade-Insecure-Requests': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36 OPR/64.0.3417.61',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://10.184.55.233:8080/^$',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9',
}
data = {
'IWEDITUSERNAME': 'WARD^',
'IWEDITPASSWORD': 'WARD^',
'IWBUTTONLOGIN': '^',
'IWBUTTONCHANGE': '^',
'IWBUTTONCANCEL': '^',
'IWEDITERROR': '^',
'IW_Action': 'IWEDITPASSWORD^',
'IW_ActionParam': '^',
'IW_FormName': 'LoginForm^',
'IW_FormClass': 'TLoginForm^',
'IW_width': '843^',
'IW_height': '757^',
'IW_TrackID_': '26'
}
response = requests.post('http://10.184.55.233:8080/%5E$', headers=headers, cookies=cookies, data=data, verify=False)
#################### This is the curl for the search page after having logged in ##############
import requests
cookies = {
'IW_CookieCheck_': 'Enabled',
'IW_CustomTrackID': '0cse19b15u30qf1bkqfu017cta6q',
'IntraWeb_WWDisa': '0cse19b15u30qf1bkqfu017cta6q_',
}
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Origin': 'http://10.184.55.233:8080',
'Upgrade-Insecure-Requests': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36 OPR/64.0.3417.61',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://10.184.55.233:8080/$',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9',
}
data = {
'IWLISTBOXPERIOD': '0^',
'IWLISTBOXWARD': '0^',
'IWLISTBOXLOCATION': '3^',
'Name3': '^',
'name1': 'IWRadioButtonExact^',
'Name2': '^',
'IWEDITFILENO': '^',
'IWEDITNAME': 'Monwabisi^',
'IWEDITINITS': '^',
'IWEDITAGE': '^',
'IWEDITSEX': '^',
'IWEDITDOB': '^',
'IWEDITID': '^',
'IWEDITTESTS': '^',
'IWBUTTONSEARCH': '^',
'IWBUTTONRESET': '^',
'IWGRIDMYPATIENTS': '^',
'IWEDIT1': '^',
'IW_Action': 'IWLISTBOXLOCATION^',
'IW_ActionParam': '^',
'IW_FormName': 'MyPatientsForm^',
'IW_FormClass': 'TMyPatientsForm^',
'IW_width': '843^',
'IW_height': '757^',
'IW_TrackID_': '13'
}
response = requests.post('http://10.184.55.233:8080/%5E$', headers=headers, cookies=cookies, data=data, verify=False)
####################### This is the curl for the patient page:
import requests
cookies = {
'IW_CookieCheck_': 'Enabled',
'IW_CustomTrackID': '0cse19b15u30qf1bkqfu017cta6q',
'IntraWeb_WWDisa': '0cse19b15u30qf1bkqfu017cta6q_',
}
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Origin': 'http://10.184.55.233:8080',
'Upgrade-Insecure-Requests': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36 OPR/64.0.3417.61',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://10.184.55.233:8080/^$',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9',
'If-Modified-Since': 'Tue, 8 Oct 2019 12:38:34 GMT',
}
data = {
'IWGRIDHISTORY': '^',
'IWBUTTONSAVEPDF': '^',
'IWTABCONTROL1_input': '1^',
'IW_Action': 'IWTABCONTROL1^',
'IW_ActionParam': '^',
'IW_FormName': 'AccessForm^',
'IW_FormClass': 'TAccessForm^',
'IW_width': '1307^',
'IW_height': '1021^',
'IW_TrackID_': '23'
}
response = requests.post('http://10.184.55.233:8080/%5E$', headers=headers, cookies=cookies, data=data, verify=False)
"""