It is a python automated script that downloads all videos of a particular TikTok user at once. As we know that web scraping doesn't work with BeautifulSoup for Dynamic pages in which that code go on updating. So, I used the selenium web browser to scrape videos from TikTok.
These should run without any error:
- All modules according to requirements
pip install -r requirements.txt
- Chrome webdriver should be installed
- Path of Chrome Browser should be added to PATH
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import urllib.request
Being a dynamic site we have to let load all the data present on the site so we have to scroll to bottom to load all the videos on the page of that particular user
def scroll(SCROLL_PAUSE_TIME, WAIT):
print('Started Scrolling..')
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);")
sleep(SCROLL_PAUSE_TIME)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
sleep(WAIT)
print('Reached to the end of page')
def download_video(url, num_vid):
urllib.request.urlretrieve(url, 'media' + str(num_vid) + '.mp4')
print('Media' + str(num_vid) + '.mp4 downloaded.')
As TikTok provides the video link in the source code of the webpage when we hover the cursor over each post so we use
def hover(element):
hover = ActionChains(driver).move_to_element(element)
hover.perform()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
Free Software, Hell Yeah!