【发布时间】:2020-11-21 17:44:50
【问题描述】:
我正在尝试从格式为https://www.youtube.com/c/%s/videos %accountName 的 youtube 帐户中抓取 Youtube URL + 标题。例如Apple
Youtube 中可点击文本(标题)的类是ytd-grid-video-renderer #video-title.yt-simple-endpoint.ytd-grid-video-renderer - 在检查器模式下单击标题对象时(Firefox)
我没有得到任何结果,但 URL 'url'(在 webCommandMetadata 中的某处)和标题'simpleText' 显示在 request.content 中
例子:
url = "https://www.youtube.com/c/%s/videos" % account
req = requests.get(url, timeout=30)
soup = BeautifulSoup(req.content, 'html.parser')
# latest_videos_html = soup.select('.yt-lockup-content:not(:has(span.yt-uix-livereminder)) .yt-lockup-title a')[:6]
# latest_videos_html = soup.select('.yt-lockup-content:not(:has(span.yt-uix-livereminder)) .yt-simple-endpoint a')[:18]
latest_videos_html = soup.select('ytd-grid-video-renderer #video-title.yt-simple-endpoint.ytd-grid-video-renderer')[:18]
print(latest_videos_html)`
我的问题是:我怎么知道在soup.select 中输入什么以及如何调试它以便我将来自己解决这个问题?
感谢您的支持!
【问题讨论】:
-
你必须用 selenium 替换请求。请求不会从 youtube 加载 javascript 代码
-
您可以考虑查看 youtube-dl 如何抓取 YouTube 页面。它也是用 Python 编写的。
标签: python web-scraping beautifulsoup request