Search code examples
pythonselenium-webdriverweb-scrapinguser-input

How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python)


First, I apologize if I make/made any mistakes, or sound silly. I have only just started learning python about a week ago and I'm trying to build a web scraping program with the little knowledge I currently have. There may be some useless time.sleep functions and things, but I have changed and tested many things to get my script to work, so please ignore those things, unless there are critical errors in my code, or stuff I can improve.

So I have written this script using selenium with intent to take video links from the app "TikTok," and run them through a TikTok video downloader website to download these videos. I have successfully written a script that can be used with one link at a time. For example, the user inputs a link and the script opens the website, pastes the link, submits, and then downloads the video from the link.

I am wondering how I can turn this script into one where the user may input many many links, (up to 100) and repeat the downloading process until there are no more links. I would also like for my program to display the current link in use on the terminal, and say "Successfully Downloaded" every time a video is downloaded. My current script code is below. Any help is greatly appreciated. Thank you.

# imports
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options

# set chrome options
options = Options()
options.headless = False

# path of chrome webdriver
PATH= 'C:\Program Files (x86)\chromedriver.exe'

# print text and ask for user link
print('TikTok Auto Download Links BOT')
print('-' * 30)
link_url = input('Enter link to be downloaded: ')

# set browser driver and open window with select size
browser = webdriver.Chrome(PATH, options = options)
browser.set_window_size(1920, 1080)
browser.maximize_window()
browser.get('https://snaptik.app/en-us')

time.sleep(1)

# find url box and paste user's input then submit
url_field = browser.find_element_by_id('url')
url_field.send_keys(link_url)
url_field.submit()

time.sleep(3)

# click download button
download_button = browser.find_element_by_xpath('//*[@id="div_download"]/section/div/div/div/article/div[2]/div/a[1]')
time.sleep(3)
download_button.click()

# print successful for UI
time.sleep(3)
print('Download successful! ')

# wait before closing
time.sleep(3)

# close browser 
browser.quit()

Solution

  • from selenium import webdriver
    import time
    from selenium.webdriver.chrome.options import Options
    
    # set chrome options
    options = Options()
    options.headless = False
    
    # path of chrome webdriver
    PATH= 'C:/Users/COUNT DEXTER/Downloads/chromedriver_win32/chromedriver.exe'
    
    # print text and ask for user link
    print('TikTok Auto Download Links BOT')
    print('-' * 30)
    
    set_download_limit = 100 #download limit
    i=1 #counter
    while i<=set_download_limit: #while condition
        link_url = input('Enter link to be downloaded: ')
    
        # set browser driver and open window with select size
        browser = webdriver.Chrome(PATH, options = options)
        browser.set_window_size(1920, 1080)
        browser.maximize_window()
        browser.get('https://snaptik.app/en-us')
    
        time.sleep(1)
    
        # find url box and paste user's input then submit
        url_field = browser.find_element_by_id('url')
        url_field.send_keys(link_url)
        url_field.submit()
    
        time.sleep(3)
    
        # click download button
        download_button = browser.find_element_by_xpath('//*[@id="div_download"]/section/div/div/div/article/div[2]/div/a[1]')
        time.sleep(3)
        download_button.click()
    
        # print successful for UI
        time.sleep(3)
        print('Download successful! ')
        i+=1 #increment counter, then return to loop
    
    # wait before closing
    time.sleep(3)
    
    # close browser 
    browser.quit()```