我如何从用户那里获得多个(大量)输入,并对用户给出的每个输入重复代码?(Python)



首先,如果我犯了/犯了任何错误,或者听起来很傻,我深表歉意。大约一周前,我才刚刚开始学习python,我正试图用我目前所掌握的一点知识来构建一个网络抓取程序。可能有一些无用的time.sleep函数之类的东西,但我已经更改和测试了很多东西来让我的脚本正常工作,所以请忽略这些东西,除非我的代码中有严重错误,或者我可以改进的东西。

所以我用selenium编写了这个脚本,目的是从应用程序"中获取视频链接;TikTok;并通过TikTok视频下载网站运行它们来下载这些视频。我已经成功地编写了一个脚本,可以一次使用一个链接。例如,用户输入链接,脚本打开网站,粘贴链接,提交,然后从链接下载视频。

我想知道如何将这个脚本变成一个用户可以输入许多链接(最多100个(并重复下载过程直到没有更多链接的脚本。我还希望我的程序在终端上显示当前使用的链接;成功下载";每次下载视频时。我当前的脚本代码如下。非常感谢您的帮助。非常感谢。

# 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()
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()```

相关内容

  • 没有找到相关文章

最新更新