from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from urllib.parse import urlparse import time import openpyxl # Set the path to the WebDriver executable. driver_path = "C:/Users/yamru/OneDrive/Desktop/Filmora/chromedriver_win32/chromedriver.exe" # Create a new instance of the Chrome driver. driver = webdriver.Chrome(executable_path=driver_path) def process_subdiv(username): url = 'https://tokcounter.com/tiktok-video-downloader' driver.get(url) time.sleep(5) # Find the button element by XPath and click it. button_xpath1 = '//*[@id="__next"]/div/div[1]/button' button_element1 = driver.find_element(By.XPATH, button_xpath1) button_element1.click() # Find the input element by XPath and enter the username. input_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[1]/input' input_element = driver.find_element(By.XPATH, input_xpath) input_element.clear() # Clear the input box input_element.send_keys(username) # Find the button element by XPath and click it. button_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[1]/button' button_element = driver.find_element(By.XPATH, button_xpath) button_element.click() time.sleep(5) # Maximum wait time of 10 seconds # Wait for the div element with class name "mt-3" to be present. div_xpath = '//div[@class="mt-3"]' wait = WebDriverWait(driver, 10) div_element = wait.until(EC.presence_of_element_located((By.XPATH, div_xpath))) # Find all subdiv elements within the div element. subdiv_elements = div_element.find_elements(By.TAG_NAME, 'div') # Get the count of subdiv elements. subdiv_count = len(subdiv_elements) # Divide the count by 5 and convert to an integer. new_data = int(subdiv_count / 5) print(new_data) # Create a new Excel workbook and select the active sheet. workbook = openpyxl.Workbook() sheet = workbook.active for i in range(1, new_data + 1): start_time = time.time() # Start time for the iteration time.sleep(5) # Find the input element by XPath and enter the username. input_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[1]/input' input_element = driver.find_element(By.XPATH, input_xpath) input_element.clear() # Clear the input box input_element.send_keys(username) # Find the button element by XPath and click it. button_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[1]/button' button_element = driver.find_element(By.XPATH, button_xpath) button_element.click() time.sleep(5) # Maximum wait time of 10 seconds subdiv_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[2]/div[{}]/div'.format(i) subdiv_element = driver.find_element(By.XPATH, subdiv_xpath) # Scroll the page to bring the element into view. driver.execute_script("arguments[0].scrollIntoView();", subdiv_element) time.sleep(0.5) subdiv_element.click() time.sleep(5) # Maximum wait time of 10 seconds # Find the element by XPath and get the text. description_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[4]/p' description_element = driver.find_element(By.XPATH, description_xpath) description = description_element.text # Find the element by XPath and extract the URL. url_xpath = '//*[@id="__next"]/div/div/div[3]/div[2]/div[4]/div/div[2]/a[1]' url_element = driver.find_element(By.XPATH, url_xpath) url = url_element.get_attribute('href') # Extract the text after the last slash in the URL. video_id = urlparse(url).path.rsplit('/', 1)[-1] # Print the video_id, description, and url. # print("Description:", description) # print("Video ID:", video_id) # print("URL:", url) # Write the data into the Excel file. sheet.cell(row=i, column=1).value = video_id sheet.cell(row=i, column=2).value = description sheet.cell(row=i, column=3).value = url end_time = time.time() # End time for the iteration iteration_time = end_time - start_time # Time taken for the iteration print("Time for iteration {}: {} seconds".format(i, iteration_time)) print() # Save the Excel file with the username as the filename. filename = username workbook.save(filename + ".xlsx") # Define the list of usernames. usernames = ['kiranbnl1', 'manhwa.jas', 'phoeia', 'fwairy._cqxx', 'mythosa', 'mangwha', 'whigs.k', 'helloguy555', 'mlwlise', 'cviiaa_', 'animemanhwaedits', 'ggusionkof', '.tez', 'sankaai_', 'youssef_1070_', 'morfaee', 'leopardo355', 'akuma_sfc', 'makima__anime', 'amonorei', 'housedesign77', 'larnet1', 'neyller_', 'yamato_da', 'twlxty', 'mikuchan__1', 'qwizi_xq'] # Process each username. for username in usernames: process_subdiv(username) print("Data extracted succeessfully from", username) print() # Close the driver. driver.quit()