YouTube Search Automation
Today is Saturday and it’s the fourteenth day of my self-quarantine. I woke up a bit late and I got the news that the IMF has declared, we are in a recession now. I open my Mac. As I open it, the first thing that came in my mind is to watch the latest updates of the financial market and related news. I was being lazy and there comes the father of all evil, an idea. An idea to automate the process of (1) opening youtube, (2) start typing (worst thing for a morning mood) and (3) Click on Search, to get the market news. And what next, I kept everything aside and start working on it.
Install Selenium:
pip install -U selenium
For some Mac users, it can also be:
pip3 install selenium
or
sudo pip3 install selenium
Confirm it is there:
pip freeze
Install Homebrew:
Either: https://sites.google.com/a/chromium.org/chromedriver/downloadsor:
https://devopscraft.com/how-to-install-homebrew-on-macos-catalina/
After installing Homebrew, we need to get ChromeDrive:
brew tap homebrew/cask && brew cask install chromedriver
Import Webdriver:
from selenium import webdriverdriver = webdriver.Chrome()
Navigate to URL:
driver.get(“https://www.youtube.com")
Go to Search Box and Type
Get the xpath id from the YouTube → Inspect → Select Elementwise inspection
Select the typing portion and get the id by right-clicking in the highlighted code and select by_xpath.
searchbox = driver.find_element_by_xpath(‘//*[@id=”search”]’)searchbox.send_keys(‘Market Updates ET’)
Similarly, get the id for the Search Click button by selecting exactly the search symbol.
Click on the Search Button
SearchButton = driver.find_element_by_xpath(‘//*[@id=”search-icon-legacy”]/yt-icon’)SearchButton.click()
And it’s done.
There is plenty of scope of improvement but for now, it serves my purpose and will make my morning smoother. Now, I won’t need to worry about my personalized news every morning, with one command, I will have my news ready.
Thanks to Python ;)
References:
- No module named selenium: https://stackoverflow.com/questions/48267633/python-no-module-named-selenium
- Installing Homebrew: https://devopscraft.com/how-to-install-homebrew-on-macos-catalina/
- Getting ChromeDriver: https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver