Reputation: 21
I want to get a list of video URLs from a specific YouTube channel with Python.
First, I wrote the following code.
import os
import time
import requests
import pandas as pd
API_KEY = os.environ['API_KEY']
CHANNEL_ID = 'your_searching_channel_id'
base_url = 'https://www.googleapis.com/youtube/v3'
url = base_url + '/search?key=%s&channelId=%s&part=snippet,id&order=date&maxResults=50'
infos = []
But I don't know the next step.
I read YouTube API HELP, but I couldn't understand how to structure the program.
Upvotes: 1
Views: 5918
Reputation: 128
Logic ->
Import Necessary libraries.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
Use Selenium. I'm picking up Chrome driver.Make sure your chromedriver is in same directory in which you are running the script.
driver = webdriver.Chrome()
Supply the Youtube Channel's url.
url = input("Enter Youtube Channel URL : ")
Go to the videos section.
driver.get(url+"/videos")
Scroll down till the end of the page. Because the page loads when scrolled down,we need to take care of this.
ht=driver.execute_script("return document.documentElement.scrollHeight;")
while True:
prev_ht=driver.execute_script("return document.documentElement.scrollHeight;")
driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
time.sleep(2)
ht=driver.execute_script("return document.documentElement.scrollHeight;")
if prev_ht==ht:
break
Find links of all the videos.
links=driver.find_elements_by_xpath('//*[@id="video-title"]')
for link in links:
print(link.get_attribute("href"))
That's it.
Cons- Requires a webdriver. Slower than getting results directly from Youtube API.
Upvotes: 8
Reputation: 81
The solution provided assumes that the link given to driver.get() is a Youtube Playlist. Modify it at your discretion.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("pathToChromeDriver")
driver.get('https://www.youtube.com/watch?v=WN-IW6wOdnI&list=PLs4hTtftqnlBOsOiQ2U1fddASTO9SlRbY')
def collectLinks():
elements = []
elems = driver.find_elements_by_xpath("//a[@href]")
print("Collecting links...")
for elem in elems:
elements.append(str(elem.get_attribute("href")))
for i in elements:
print(i)
collectLinks()
driver.quit()
Where "pathToChromeDriver" is replaced by your path to chrome driver.
Where 'https://www.youtube.com/watch?v=WN-IW6wOdnI&list=PLs4hTtftqnlBOsOiQ2U1fddASTO9SlRbY' is replaced by the playlist you want to find all url links.
Upvotes: 1