Get title from YouTube videos

前端 未结 17 1952
抹茶落季
抹茶落季 2020-12-07 17:30

I want to extract the Title of YouTube\'s videos. How can I do this?

Thanks.

17条回答
  •  情歌与酒
    2020-12-07 18:00

    Similarly to Matej M, but more simply:

    import requests
    from bs4 import BeautifulSoup
    
    
    def get_video_name(id: str):
        """
        Return the name of the video as it appears on YouTube, given the video id.
        """
        r = requests.get(f'https://youtube.com/watch?v={id}')
        r.raise_for_status()
        soup = BeautifulSoup(r.content, "lxml")
        return soup.title.string
    
    
    if __name__ == '__main__':
        js = get_video_name("RJqimlFcJsM")
        print('\n\n')
        print(js)
    

提交回复
热议问题