-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scraper.py
222 lines (195 loc) · 9.74 KB
/
Scraper.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
import time
from datetime import datetime
import cv2
import pytesseract
from msedge.selenium_tools import EdgeOptions
import concurrent.futures
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import numpy as np
import ctypes
import re
class Scraper():
def __del__(self):
self.driver.quit()
def __init__(self, data):
self.start = time.time()
print(time.time() - self.start)
self.data = data
self.TESS_PATH = 'D:\Software\Programming\Teserract_OCR\\tesseract.exe'
self.TESS_CONFIG = "-c tessedit_char_whitelist=2345678abcdefghkmnprwxy --psm 8"
self.DRIVER_PATH = "D:\Software\Programming\Edge Webdriver\msedgedriver.exe"
self.WEBSITE = "https://vahan.nic.in/nrservices/faces/user/citizen/citizenlogin.xhtml"
self.LOGIN_ID = "9403675041"
self.PASSWORD = "Shashikant1@"
isStop = False
options = EdgeOptions()
# options.add_argument("--headless")
options.use_chromium = True
# options.add_argument("--headless")
options.add_argument("disable-gpu")
options.add_argument("--window-size=1920,1200")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--disable-extensions")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument('--disable-browser-side-navigation')
self.driver = webdriver.Chrome(executable_path=self.DRIVER_PATH, options=options)
pytesseract.pytesseract.tesseract_cmd = self.TESS_PATH
self.count = 0
while not isStop:
try:
self.login()
self.driver.find_element_by_name("regn_no1_exact").click()
self.send_discrete_keys(name="regn_no1_exact", string=data)
m = self.captchaSolve()
if m == False:
break
self.count+=1
if self.count >= 20:
self.driver.quit()
isStop = True
break
while (time.time() - self.start) <= 110:
print(time.time() - self.start)
try:
WebDriverWait(self.driver, 3).until(EC.presence_of_element_located(
(By.XPATH, "//*[@id='rcDetailsPanel']/div[1]/table/tbody/tr[4]/td/div/span[1]")))
cv2.imwrite(f"C:\\Users\\soham\\OneDrive\\Desktop\\Dataset\\{self.captchaText}.png", self.im)
self.driver.quit()
isStop = True
break
except Exception as e:
cv2.imwrite(f"C:\\Users\\soham\\OneDrive\\Desktop\\notset\\{self.captchaText}.png", self.im)
# print(1, e)
WebDriverWait(self.driver, 2).until(EC.presence_of_element_located(
(By.XPATH, "//*[@id='userMessages']/div/ul/li/span")))
try:
text = self.driver.find_element_by_xpath(
"//*[@id='userMessages']/div/ul/li/span").text
if text == 'Vehicle Detail not found':
# print('broke')
break
except Exception as e:
print(4, 'nothibng found')
wait = WebDriverWait(self.driver, 10)
print('waiting')
wait.until(EC.element_to_be_clickable((By.NAME, "vahancaptcha:btn_Captchaid")))
self.driver.find_element_by_name("vahancaptcha:btn_Captchaid").send_keys(Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.NAME, "vahancaptcha:btn_Captchaid")))
print('refreshed')
m = self.captchaSolve()
if m == False:
break
self.count+=1
if self.count >= 20:
self.driver.quit()
isStop = True
break
isStop = True
# else:
# print((time.time() - self.start))
# ctypes.windll.user32.MessageBoxW(0, "Soham text", f"Your {time.time() - self.start}", 1)
# self.driver.quit()
# break
except Exception as e:
# print(2, e)
isStop = False
self.driver.quit()
def login(self):
self.driver.get(self.WEBSITE)
self.driver.find_element_by_name("TfMOBILENO").click()
self.send_discrete_keys(name="TfMOBILENO", string=self.LOGIN_ID)
self.driver.find_element_by_name("btRtoLogin").click()
wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_element_located((By.NAME, "tfPASSWORD")))
self.send_discrete_keys(name="tfPASSWORD", string=self.PASSWORD)
self.driver.find_element_by_name("btRtoLogin").click()
def captchaSolve(self):
wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "vahancaptcha:ref_captcha")))
self.driver.find_element_by_name("regn_no1_exact").click()
self.count+=1
if self.count >= 20:
self.driver.quit()
isStop = True
return False
self.send_discrete_keys(name="regn_no1_exact", string=self.data)
# print('in captcha')
self.captchaText = self.tesseract_captcha_ocr()
# print(self.captchaText, " ", len(self.captchaText))
if len(self.captchaText) == 5:
# time.sleep(1)
self.driver.find_element_by_id("vahancaptcha:CaptchaID").send_keys(Keys.ENTER)
self.send_discrete_keys(name="vahancaptcha:CaptchaID", string=self.captchaText, byType=By.ID)
time.sleep(1)
try:
WebDriverWait(self.driver, 3).until(EC.element_to_be_clickable(
(By.XPATH, "//button[normalize-space()='Vahan Search']")))
self.driver.find_element_by_xpath("//button[normalize-space()='Vahan Search']").click()
except Exception as e:
time.sleep(1)
# print(3, e)
WebDriverWait(self.driver, 3).until(EC.element_to_be_clickable(
(By.XPATH, "//button[normalize-space()='Vahan Search']")))
self.driver.find_element_by_xpath("//button[normalize-space()='Vahan Search']").send_keys(
Keys.ENTER)
# print('captcha')
return True
else:
return self.captchaSolve()
def send_discrete_keys(self, name, string, byType=By.NAME):
self.driver.find_element(byType, name).clear()
for character in string:
self.driver.find_element(byType, name).send_keys(character)
def tesseract_captcha_ocr(self):
wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#vahancaptcha\:ref_captcha")))
img = self.driver.find_element_by_css_selector("#vahancaptcha\:ref_captcha").screenshot_as_png
# img = self.driver.find_element_by_id("vahancaptcha:ref_captcha").screenshot_as_png
x = np.fromstring(img, dtype='uint8')
# decode the array into an image
self.captchaImg = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
self.im = self.captchaImg[3:48 , 3:129]
# cv2.imwrite(f"C:\\Users\\soham\\OneDrive\\Desktop\\Dataset\\ss.png", im)
while cv2.waitKey(5000) & 0xFF == ord('q'):
break
gray = cv2.cvtColor(self.captchaImg, cv2.COLOR_BGR2GRAY)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
gray = cv2.medianBlur(gray, 3)
text = pytesseract.image_to_string(gray, lang='eng', config=self.TESS_CONFIG)
if re.match(".*[019ijloqstuvz].*",text):
cv2.imwrite(f"C:\\Users\\soham\\OneDrive\\Desktop\\notset\\{self.captchaText}.png", self.im)
if len(text) <5:
cv2.imwrite(f"C:\\Users\\soham\\OneDrive\\Desktop\\crips\\{self.captchaText}.png", self.im)
return text[:5]
def scraper():
cnt =0
while(True):
start = time.time()
try:
with concurrent.futures.ProcessPoolExecutor(max_workers = 1) as multiProcessExecutor:
for i in range(200):
multiProcessExecutor.submit(Scraper,"MH14DT2660")
if (time.time() - start) >= 500:
# ctypes.windll.user32.MessageBoxW(0, "Soham text", f"Your {cnt}", 1)
multiProcessExecutor.shutdown(wait=False)
break
else:
print((time.time() - start))
# multiProcessExecutor.shutdown(wait=False)
# if (time.time() - start) >= 400:
# # for i in range(12):
# multiProcessExecutor.shutdown(wait=False)
except:
print("excepted")
cnt+=1
print(f"Your {cnt}")
# ctypes.windll.user32.MessageBoxW(0, "Soham text", f"Your {cnt}", 1)
#
#
if __name__ == "__main__":
scraper()