'How do you scrape titles and amount of views using rvest

I want to scrape information on Youtube without using YouTube api. I tried using the code below using xpath but it returns character(empty).

html <- read_html("https://www.youtube.com/watch?v=WRz2MxhAdJo")
title <- html_nodes(html,xpath = '//*[@id="container"]/h1') %>% html_text()


Solution 1:[1]

You can extract the title and views from the meta tags from the webpage.

The title is in the <meta name="title" tag.

Example:

<meta name="title" content="Avicii - Without You “Audio” ft. Sandro Cavazza">

The views (called interaction) is in the <meta itemprop="interactionCount" tag.

Example:

<meta itemprop="interactionCount" content="160908331">

For this, you can use something like shown in this answer:

html %>% html_nodes("[name='title']") %>% html_attr("content") 
html %>% html_nodes("[itemprop='interactionCount']") %>% html_attr("content") 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco Aurelio Fernandez Reyes