【发布时间】: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