【问题标题】:How can I get the link to YouTube Channel from Video Page?如何从视频页面获取 YouTube 频道的链接?
【发布时间】:2021-09-07 01:28:00
【问题描述】:

我一直在尝试从视频页面获取 YouTube 频道的链接。但是,我找不到链接的元素。使用 Inspector,很明显链接就在此处,如下图所示。

使用代码'a.yt-simple-endpoint.style-scope.yt-formatted-string',我尝试通过以下代码获取链接。

! 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"))

但是,无论我使用links = soup.select('a.yt-simple-endpoint.style-scope.yt-formatted-string') 还是links = soup.find('a', class_='yt-simple-endpoint style-scope ytd-video-owner-renderer'),它都没有打印任何内容。请有人帮我解决这个问题。

【问题讨论】:

    标签: selenium beautifulsoup youtube webdriver


    【解决方案1】:

    而不是这个:

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

    如果我愿意,请在Selenium

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

    【讨论】:

    • 它有效。谢谢你。我只是按照您的建议将其替换为 links = driver.find_elements_by_css_selector('#text > a')
    猜你喜欢
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    相关资源
    最近更新 更多