CanaryTheBird
CanaryTheBird

Reputation: 259

How can I get the link to YouTube Channel from Video Page?

I've been trying to get the link to the YouTube channel from the Video Page. However, I couldn't find the element of the link. With Inspector, it is obvious that the link is right here as the following picture. enter image description here

With the code 'a.yt-simple-endpoint.style-scope.yt-formatted-string', I tried to get the link through the following code.

! pip install selenium
from selenium import webdriver
! pip install beautifulsoup4
from bs4 import BeautifulSoup
driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
driver.get('https://www.youtube.com/watch?v=P6Cc2R2jK6s')
soup = BeautifulSoup(driver.page_source, 'lxml')
links = soup.select('a.yt-simple-endpoint.style-scope.yt-formatted-string')
for link in links:
    print(link.get_attribute("href"))

However, no matter I used links = soup.select('a.yt-simple-endpoint.style-scope.yt-formatted-string') or links = soup.find('a', class_='yt-simple-endpoint style-scope ytd-video-owner-renderer'), it did not print anything. Someone please help me solve this.

Upvotes: 1

Views: 371

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

Instead of this:

links = soup.select('a.yt-simple-endpoint.style-scope.yt-formatted-string')

In Selenium if I would do:

links = drvier.find_elements_by_css_selector('a.yt-simple-endpoint.style-scope.yt-formatted-string')

Upvotes: 1

Related Questions