【问题标题】:Use beautifulsoup to get a youtube video‘s information使用 beautifulsoup 获取 youtube 视频的信息
【发布时间】:2020-12-03 17:20:36
【问题描述】:

我是 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)

但它没有得到视频的标题。这也不适用于获取视频描述和 veido 喜欢和观看。有人可以帮我找到我应该如何搜索那些使用beautifulsoup的人吗?非常感谢!!!

【问题讨论】:

    标签: python html beautifulsoup scripting youtube


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2015-08-10
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2012-12-14
      • 2013-04-04
      • 1970-01-01
      相关资源
      最近更新 更多