-
Notifications
You must be signed in to change notification settings - Fork 0
/
test 6.py
241 lines (189 loc) · 9.05 KB
/
test 6.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
import cv2
import urllib.request as urllib
import numpy as np
from selenium import webdriver
import os
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Chrome()
driver.get('https://www.edmunds.com/')
# New Cars Click
first_click = driver.find_element_by_xpath('//*[@id="nav_new_future_makes"]')
first_click.click()
# time.sleep(1)
# CarNameList in second click
second_click = driver.find_elements_by_xpath('//*[@data-tracking-id="nav_mmy_select_make"]')
item_list = []
# fetching item text in list(car Brand name list)
i = 0
for item in second_click:
text1 = item.text
item_list.append(text1)
# eliminating spaces from list
item_list = [x for x in item_list if x != '']
print(item_list)
item_list_capital = item_list
# Lower case item list
item_list = [x.lower() for x in item_list]
# Eliminating Spaces and adding - between them
k = []
for hh in item_list:
j = hh.replace(' ', '-')
k.append(j)
item_list = k
print(item_list)
#################
"""
#To create folders
while item_list:
os.mkdir(os.getcwd()+"/"+str(item_list[i]), 0o777)
i = i + 1
"""
# ----------------------------------------theProgramBegins------------------------------------------------
i = 2
while i < len(item_list):
xpath_send = '//*[@id="nav"]/div/div/div/div[1]/div[2]/div[2]/a[@href="/' + item_list[i] + '/"]'
second_click_text = driver.find_element_by_xpath(xpath_send)
print(item_list[i])
print(xpath_send)
second_click_text.click()
time.sleep(3)
# ------------------------Creation of third list and filtering-------------------------------------------
third_layer = driver.find_elements_by_xpath('//*[@data-tracking-id="nav_mmy_model_select"]')
third_list = []
for item in third_layer:
text1 = item.text
third_list.append(text1)
print(third_list)
# print(len(third_list))
# fourth layer compilation start
h = 0 # 0
while h < len(third_list):
try:
xpath_send2 = '//*[@id="nav"]/div/div/div/div[1]/div[2]/div[2]/a[' + str(h + 1) + ']'
print(xpath_send2)
third_click_text = driver.find_element_by_xpath(xpath_send2)
print(third_list[h])
third_click_text.click()
time.sleep(3)
except:
print("third list error " + third_list[h])
# creating directory for years
fourth_layer = driver.find_elements_by_xpath('//*[@data-tracking-id="nav_mmy_year_select"]')
fourth_list = []
print("------" + str(len(fourth_layer)))
jk = 0
for item in fourth_layer:
text32 = item.text
y = 0
if int(text32) == 2018:
fourth_list.append(text32)
print("text32:" + str(text32))
print("fourth List item y :" + str(fourth_list[y]))
y = y + 1
for item in fourth_list:
print("value of jk: " + str(jk))
fourth_layer[jk].click()
# ------------------------------------------ downloading image-------------------------------------------------------------------------------
def dwnld_img(a, b, c, sc, tc):
first_click32 = driver.find_element_by_xpath(
'/html/body/div[1]/div/main/div[3]/div/div[1]/div/div[1]/div[2]/div/button/img').click()
time.sleep(5)
try:
get_data = driver.find_element_by_xpath(
'/html/body/div[5]/div/div[1]/div/div/div/div/div[2]/div/div/div[1]/div/div[2]/div[2]/div[1]/p').text
print(get_data)
print(type(get_data))
getdata = get_data.split()
gkam = int(getdata[3])
print(gkam)
except:
print("Unable to find integer")
# -----------------------------------------------------------------download Code--------------------------------------------
tt = 1
if gkam > 100:
gkam = 100
while tt < gkam:
try:
img = driver.find_element_by_xpath(
'/html/body/div[5]/div/div[1]/div/div/div/div/div[2]/div/div/div[1]/div/div[2]/div[1]/div/div[1]/img')
src = img.get_attribute('src')
url = src
tag = os.getcwd() + "/" + str(a) + "/" + b + "/" + \
c + "/" + str(third_list[h]) + str(tt) + ".jpg"
if not os.path.exists(tag):
req = urllib.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1) # 'Load it as it is'
# cv2.imshow('lalala', img)
try:
location = os.getcwd() + "/" + str(a) + "/" + b + "/" + \
c + "/"
cv2.imwrite(location + str(third_list[h]) + str(tt) + ".jpg", img)
except:
print("bound error")
try:
button_path = driver.find_element_by_xpath(
"//*[@class='right carousel-control']").click()
except:
print("path not found 1")
button_path = driver.find_element_by_xpath(
'/html/body/div[5]/div/div[1]/div/div/div/div/div[2]/div/div/div[1]/div/div[2]/div[1]/div/div[1]/div[4]/button').click()
# if cv2.waitKey() & 0xff == 27: quit()
else:
print("already exist")
try:
button_path = driver.find_element_by_xpath(
'/html/body/div[5]/div/div[1]/div/div/div/div/div[2]/div/div/div[1]/div/div[2]/div[1]/div/div[1]/div[4]/button').click()
except:
button_path = driver.find_element_by_xpath(
"//*[@class='right carousel-control']").click()
print("path not found 2")
except:
print("error")
try:
button_path = driver.find_element_by_xpath(
'/html/body/div[5]/div/div[1]/div/div/div/div/div[2]/div/div/div[1]/div/div[2]/div[1]/div/div[1]/div[4]/button').click()
except:
button_path = driver.find_element_by_xpath(
"//*[@class='right carousel-control']").click()
print("path not found 3")
tt = tt + 1
try:
close_button = driver.find_element_by_xpath(
'/html/body/div[5]/div/div[1]/div/div/div/button').click()
except:
print("X button path not found 4")
driver.refresh()
time.sleep(1)
try:
first_click_1 = driver.find_element_by_xpath('//*[@id="nav_new_future_makes"]')
first_click_1.click()
time.sleep(2)
second_click45 = driver.find_element_by_xpath(sc)
second_click45.click()
time.sleep(2)
third_click45 = driver.find_element_by_xpath(tc)
third_click45.click()
time.sleep(3)
except:
first_click_1 = driver.find_element_by_xpath('//*[@id="nav_new_future_makes"]')
while first_click.click() is not True:
print("not true again....")
driver.refresh()
time.sleep(5)
time.sleep(2)
second_click45 = driver.find_element_by_xpath(sc)
second_click45.click()
time.sleep(2)
third_click45 = driver.find_element_by_xpath(tc)
third_click45.click()
time.sleep(3)
dwnld_img(item_list_capital[i], third_list[h], fourth_list[0], xpath_send, xpath_send2)
jk = jk + 1
print(fourth_list)
h = h + 1
back_click = driver.find_element_by_xpath('//*[@id="nav"]/div/div/div/div[1]/div[2]/div[1]/a[2]').click()
i = i + 1
back_click = driver.find_element_by_xpath('//*[@id="nav"]/div/div/div/div[1]/div[2]/div[1]/a').click()