SASpencer SASpencer - 3 months ago 9
R Question

rvest package to harvest instagram number of followers?

I'm trying to adapt the example code in the

rvest
package for getting number of followers for an account on instagram (e.g.,
https://www.instagram.com/bradyellison/
). I tried using
selectorgadget
to isolate code for number of followers, which gave me this:
._218yx:nth-child(2) ._s53mj
. But I don't get the expected followers back, and not sure how to debug this. Here's my code.

# example
require(rvest)
html <- read_html("https://www.instagram.com/bradyellison/")
athlete_followers <- html_nodes(html, "._218yx:nth-child(2) ._s53mj")
length(athlete_followers)


Output is:

[1] 0


Expected followers are 12.1K. Would really appreciate help. (I've tried using the Instagram API for this first, but couldn't get it to work, perhaps because I'm in sandbox mode or something.)

Answer

You can't rvest this page because it's not a static site, rather it's generated dynamically via code (e.g. try xml_text(html)). To access Instagram data you should use their API. See full example here: https://www.r-bloggers.com/analyze-instagram-with-r/ .

Comments