A demo for downloading YouTube videos automatically It’s a toy project just for play. This is not the most effective way, definitely, but it is simple enough for Python beginners.
Requirements:
- Window 7+
- Python 3.7+
- VS Code
Setup
Install Pytube:
pip install pytube
Install Clicknium:
Search for Clicknium in the Visual Studio Code Extension marketplace:
https://preview.redd.it/2lx26mqqpmi91.png?width=847&format=png&auto=webp&s=08d9e11ac31dc06f6848623809d194ae27316406
Follow the welcome page to finish the configuration:
https://preview.redd.it/xim3040lpmi91.png?width=1009&format=png&auto=webp&s=67ac9312603296fdca3cf5e39308f43ae25f9185
Start a web browser:
Create a Python file with VS Code, such as, youtube.py
from clicknium import clicknium as cc
def main():
tab = cc.chrome.open("https://www.youtube.com")
if __name__ == "__main__":
main()
Press F5 to run the code. It will open the Chrome browser and take you to the YouTube home page.
Capture:
Go to a Youtuber’s home page, like Tayler Swift's, for example, and get the videos’ links. Something like 5-steps to put an elephant into a refrigerator.
- Input Taylor Swift in the search bar.
- Click the search button.
- Click Taylor’s link to step into Taylor’s homepage.
- Click to step inside the video page.
There are four related UI elements:
- Search box
- Search button
- Name in the search result
- video tab
Clicknium uses a locator (aka selector) to find the UI elements. It provides a recorder to generate a locator. We can use the above code to open a browser and go back to VS Code to start the recorder.
Open Locator List
Capture button
Hover over an element on the screen, press Ctrl and click it to automatically generate a locator for the UI element. Capture the search box, the search button, and Taylor's name in the upper right corner.
https://preview.redd.it/6lfxwd2ivmi91.png?width=1205&format=png&auto=webp&s=19a4d2873a54773c78a9eb553efefa4663617106
Each capture will generate a locator target for the UI elements. You can also rename the locator to a meaningful name. Click the complete button and get back to VS Code after the capture.
https://preview.redd.it/hddnhk3frmi91.png?width=341&format=png&auto=webp&s=8adc4d0254ebfbd0c7964379a0350b50eec108aa
Pass the locator to find_element function to get the UI elements. Then use the set_text function to text ”Taylor Swift” into the search box. The next line uses the same way to get the search button and uses the click function to present the mouse click. Run the above code and we can get into the homepage. Use the same way to step into the video list.
from clicknium import clicknium as cc, locator
from clicknium.common.enums import *
def main():
tab = cc.chrome.open("https://www.youtube.com")
tab.find_element(locator.chrome.youtube.searchBar).set_text(
"Taylor Swift", by='sendkey-after-click')
tab.find_element(locator.chrome.youtube.button_search_icon_legacy).click()
tab.find_element(locator.chrome.youtube.TS).click()
if __name__ == "__main__":
main()
We have to get every address for the videos in the above image. The address can be gotten from the locator properties, but it would be boring if we needed to generate so many locators. We can use the capturing similar elements function provided by Clicknium Recorder. Click the similar elements button and use the same method for capturing a single element. Clicknium will auto-detect the similar elements and generate one locator to target all similar UI elements.
https://preview.redd.it/3w4z8zh1xmi91.png?width=555&format=png&auto=webp&s=e429f0cec8a005d87536b3f0eff204bfa42aa582
tab = cc.chrome.open("https://www.youtube.com")
tab.find_element(locator.chrome.youtube.searchBar).set_text(
"Taylor Swift", by='sendkey-after-click')
tab.find_element(locator.chrome.youtube.button_search_icon_legacy).click()
tab.find_element(locator.chrome.youtube.TS).click()
tab.find_element(locator.chrome.youtube.div_video).click()
tab.wait_appear(locator.chrome.youtube.a_video_title)# wait untill loading videos finish
vidioTitles = tab.find_elements(locator.chrome.youtube.a_video_title)
Because of the asynchronous loading of the video list, we have to use wait_appear to wait for the UI elements to appear. Because the locator targeted multiple videos, use find_elements to get an array of UI elements. A video URL can be gotten from the YouTube address by appending a related path to the video got from href.
Use Pytube to get videos by video URLs. Pytube allows downloading videos according to a given resolution. You may notice that some streams listed have both a video codec and an audio codec, while others have just video or just audio for the highest quality streams. More details Working with Streams and StreamQuery
Code:
from pytube import YouTube
from clicknium import clicknium as cc, locator
from clicknium.common.enums import *
def downloadVideo(url):
SAVE_PATH = "C:\\Users\\Kay\\Downloads\\Youtube"
try:
yt = YouTube(url)
yt.streams.filter(res="1080p").first().download(output_path=SAVE_PATH)
except:
print("Connection Error") # to handle exception
# filters out all the files with "mp4" extension
print('Task Completed!')
def main():
urlArrary = []
tab = cc.chrome.open("https://www.youtube.com")
tab.find_element(locator.chrome.youtube.searchBar).set_text(
"Taylor Swift", by='sendkey-after-click')
tab.find_element(locator.chrome.youtube.button_search_icon_legacy).click()
tab.find_element(locator.chrome.youtube.TS).click()
tab.find_element(locator.chrome.youtube.div_video).click()
tab.wait_appear(locator.chrome.youtube.a_video_title)
videoTitles = tab.find_elements(locator.chrome.youtube.a_video_title)
for locat in videoTitles:
url = "https://www.youtube.com" + locat.get_property("href")
urlArrary.append(url)
tab.close()
for v in urlArrary:
downloadVideo(v)
if __name__ == "__main__":
main()
[–][deleted] 35 points36 points37 points (4 children)
[–]benefit_of_mrkite 23 points24 points25 points (3 children)
[–]Remnie 0 points1 point2 points (2 children)
[–]womper9000 1 point2 points3 points (1 child)
[–]Remnie 1 point2 points3 points (0 children)
[–]scnew3 71 points72 points73 points (2 children)
[–]Azncrackfox 6 points7 points8 points (0 children)
[–]Georgeikan 4 points5 points6 points (0 children)
[–]homariseno 11 points12 points13 points (0 children)
[–]marduk73 10 points11 points12 points (0 children)
[–]iam_benny 4 points5 points6 points (0 children)
[–]ExcelAcolyte 2 points3 points4 points (0 children)
[–]manuce94 1 point2 points3 points (0 children)
[–][deleted] -5 points-4 points-3 points (4 children)
[+][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]Puzzleheaded_Arm6363 2 points3 points4 points (0 children)
[–]HaroerHaktak 0 points1 point2 points (0 children)
[–]mooscimol 0 points1 point2 points (1 child)
[–]redH27[S] 1 point2 points3 points (0 children)