Click here to Skip to main content
15,867,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Python
Path="C:\Program Files (x86)\chromedriver.exe"
driver=webdriver.Chrome(Path)
wait = WebDriverWait(driver, 20)
# Update the URL of Naukri Page that you want to search ( Make Sure that the page link which you're putting must be a job listing page and it must have Next page buttons. )
driver.get("https://www.naukri.com/cyber-security-analyst-jobs?k=cyber%20security%20analyst")
count = 50  # it's the Number of Vacancy count you want to scrape.
index, new_index, i = '0', 1, 0   # This the the index variable of the elements from which data will be Scraped
# Xpaths of the various element from which data will be scraped.
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'

# the file name the of CSV file.
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):#no of jobs in a page is always 20 or less than 20

        # Here we're replacing the Old index count of Xpath with temp Index count.
        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)+".")
       # Writing all the Scrapped data into CSV file.
        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()
Posted
Updated 14-Jul-22 21:29pm
v2

1 solution

See Built-in Types (str) — Python 3.10.5 documentation[^] for details on the str class methods.
 
Share this answer
 
Comments
Nithish Karthikeyan 15-Jul-22 3:44am    
Yes I do understand the actual use of Zfill, I just want to know why have they use Zfill in this code while updating index values? What difference does it make? Also is there any alternatives instead of using zfill in this code?
Richard MacCutchan 15-Jul-22 3:54am    
How would anyone here know the answer? You should ask the people who wrote that code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900