使用 beautifulsoup 获取 youtube 视频的信息
Use beautifulsoup to get a youtube video‘s information
我是 python 脚本的新手,我正在尝试使用 beautifulsoup 获取 YouTube 视频的信息,例如标题、描述、观看次数和点赞次数。我应该如何在不使用 Youtube API 但使用 beautifulsoup 的情况下做到这一点。
现在我点击检查并点击一个 youtube 视频的标题,
<h1 class="title style-scope ytd-video-primary-info-renderer">
<yt-formatted-string force-default-style="" class="style-scope ytd-video-primary-info-renderer">
Tucker: Kamala Harris may end up running the country</yt-formatted-string></h1>
我的密码是
# I also try use class as title style-scope ytd-video-primary-info-renderer
for i in soup.findAll('h1',{'class':'style-scope ytd-video-primary-info-renderer'}):
videoName.append(i)
但是没有得到视频的标题。此外,这不适用于获取视频描述和视频点赞和观看次数。有人可以帮我找到我应该如何搜索那些使用 beautifulsoup 的人吗?非常感谢!!!
YouTube 使用 JavaScript,但 requests
不支持。所以我们可以使用像 Requests-HTML 这样的库来抓取页面。
使用 pip install requests-html
安装它。
例如,此脚本将获取视频的标题:
from requests_html import HTMLSession
from bs4 import BeautifulSoup
video_url = "ENTER LINK"
# Initialize an HTML Session
session = HTMLSession()
# Get the html content
response = session.get(video_url)
# Execute JavaScript
response.html.render(sleep=3)
soup = BeautifulSoup(response.html.html, "lxml")
print("title:", soup.select_one('#container > h1').text)
我是 python 脚本的新手,我正在尝试使用 beautifulsoup 获取 YouTube 视频的信息,例如标题、描述、观看次数和点赞次数。我应该如何在不使用 Youtube API 但使用 beautifulsoup 的情况下做到这一点。 现在我点击检查并点击一个 youtube 视频的标题,
<h1 class="title style-scope ytd-video-primary-info-renderer">
<yt-formatted-string force-default-style="" class="style-scope ytd-video-primary-info-renderer">
Tucker: Kamala Harris may end up running the country</yt-formatted-string></h1>
我的密码是
# I also try use class as title style-scope ytd-video-primary-info-renderer
for i in soup.findAll('h1',{'class':'style-scope ytd-video-primary-info-renderer'}):
videoName.append(i)
但是没有得到视频的标题。此外,这不适用于获取视频描述和视频点赞和观看次数。有人可以帮我找到我应该如何搜索那些使用 beautifulsoup 的人吗?非常感谢!!!
YouTube 使用 JavaScript,但 requests
不支持。所以我们可以使用像 Requests-HTML 这样的库来抓取页面。
使用 pip install requests-html
安装它。
例如,此脚本将获取视频的标题:
from requests_html import HTMLSession
from bs4 import BeautifulSoup
video_url = "ENTER LINK"
# Initialize an HTML Session
session = HTMLSession()
# Get the html content
response = session.get(video_url)
# Execute JavaScript
response.html.render(sleep=3)
soup = BeautifulSoup(response.html.html, "lxml")
print("title:", soup.select_one('#container > h1').text)