Could someone please explain what is the use of Zfill in the for loop while updating the index count?
While executing without the first Zfill that is with temp index I'm only able to scrape details about 3 job postings and then the for loop stops. But while trying to execute it without the 2nd zfill that is with index I don't face any noticeable issue.
Please help me resolve it.
What I have tried:
Path="C:\Program Files (x86)\chromedriver.exe"
driver=webdriver.Chrome(Path)
wait = WebDriverWait(driver, 20)
driver.get("https://www.naukri.com/cyber-security-analyst-jobs?k=cyber%20security%20analyst")
count = 50
index, new_index, i = '0', 1, 0
title_xpath = '(//*[@class="jobTuple bgWhite br4 mb-8"])['+index+']/div/div/a'
link_xpath = '(//*[@class="jobTuple bgWhite br4 mb-8"])['+index+']/div/div/a'
comp_xpath = '(//*[@class="jobTuple bgWhite br4 mb-8"])['+index+']/div/div/div/a'
exp_xpath = '(//*[@class="jobTuple bgWhite br4 mb-8"])['+index+']/div/div/ul/li[1]/span'
salary_xpath = '(//*[@class="jobTuple bgWhite br4 mb-8"])['+index+']/div/div/ul/li[2]/span'
csv_file = open('Naukri_scrape.csv', 'a', encoding="utf-8", newline='')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Title', 'Company Name', 'Vacancy Link', 'Experience Needed', 'Salary'])
while i < count:
for j in range(20):
temp_index = str(new_index).zfill(2)
title_xpath = title_xpath.replace(index, temp_index)
link_xpath = link_xpath.replace(index, temp_index)
comp_xpath = comp_xpath.replace(index, temp_index)
exp_xpath = exp_xpath.replace(index, temp_index)
salary_xpath = salary_xpath.replace(index, temp_index)
index = str(new_index).zfill(2)
try:
heading = wait.until(EC.presence_of_element_located((By.XPATH, title_xpath))).text
print(heading)
except:
heading = "Not Available"
try:
link = wait.until(EC.presence_of_element_located((By.XPATH, link_xpath))).get_attribute('href')
print(link)
except:
link = "Not Available"
try:
subheading = wait.until(EC.presence_of_element_located((By.XPATH, comp_xpath))).text
print(subheading)
except:
subheading = "NULL"
try:
experience = wait.until(EC.presence_of_element_located((By.XPATH, exp_xpath))).text
print(experience)
except:
experience = "Not Available"
try:
salary = wait.until(EC.presence_of_element_located((By.XPATH, salary_xpath))).text
print(salary)
except:
salary = "Not Disclosed"
new_index += 1
i += 1
print(str(i)+".")
csv_writer.writerow([heading, subheading, link, experience, salary])
if i >= count:
break
if i >= count:
break
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[text() = "Next"]'))).click()
new_index = 1
csv_file.close()