Scraping view count from Facebook and Youtube in real time
Note: The content of this article is now outdated, but the logic remain the same. I highly recommend Ben Collin’s article on the subject to help you achieve your data scraping goals.
Every production company wants to know if the content it produces is worth what it costs to make it. For that, there are a multitude of indicators, but the main ratio we’re looking at is of course views over budget.
Google sheets offer a simple solution to collect data from Facebook and Youtube, and I intend to share it with you!
Step 1: ♪, ♫ Do you want to build a spreadsheet? ♪, ♫
Of course, we have to start by entering our list of videos into a spreadsheet. Then, for each project, we need to write down the URLs where the videos are hosted.
For Youtube, you can copy the url from any location, wether it’s a thumbnail, or the page itself.
For Facebook, it’s a little more tricky. The only stable source (as far as I can tell from my experiments) comes from the “video” page, accessible from the left side menu. Once you are on this page, right click a thumbnail and select “copy link address”
The link is now in your clipboard, and all you have left to do is pasting it into the destination cell.
For now, our spreadsheet is as simple as this:
Step 2: Using ImportXML
One of Google Sheets’ function is called “ImportXML”. In short, it lets you import data from any website, as long as you point it in the right direction. The syntax for the formula is:
=ImportXML ( ” url ” , ” Xpath selector ” )
The “Xpath selector” is a piece of code that grabs a specific element on the page. In our case, it is going to be the number of views for these videos.
You can find this code by inspecting the source code of a page. Google Chrome is quite helpful for that, and you can find many tutorials about it online. For the sake of this articles, I will give you the solution.
Youtube Xpath: "//div[@class='watch-view-count']" Facebook Xpath: "//span[@class='fcg']"
So let’s build these formulas in cells C2 and D2:
C2: =ImportXML(B2,"//div[@class='watch-view-count']") F2: =importXML(D2,"//span[@class='fcg']")
Your spreadsheet should look something like this:
Step 3: Cleaning up the data
As you can see, the data coming from Youtube has the “views” affix, and the data coming from Facebook has that as well, and a “K” for “thousands”. If the video has a million views, it will show a “M” and will be abbreviated (ie: “1.2M”)
So before we can add these views together, we need to remove all these letters that prevent us from doing math operations.
For Youtube, I am using the “LEFT” and “LEN” functions to remove the last 6 characters (there’s a space before the word “views”). I am actually going to change the formula we have in C2 to include that in there.
For Facebook, it’s more complex. Here are all the elements that could be displayed and need to be removed:
- “views”
- the “K” when views are in the thousands
- the “M” and the “.” when view are in millions
Removing the “views” will done using the same technique as for Youtube.
Then, I am using a “VLOOKUP” to switch the “K” and “M” to “,000” and “,000,000” respectively. I created a table and named it “ViewCounter”. You can see that this named range (see more about names ranged here) on the same sheet of my example, but I would of course suggest putting that on a separate tab and hiding it.
Finally, I am using the “SUBSTITUTE” function to remove the “.”
Here are the formulas I used:
C2: =LEFT(IMPORTXML(B2,"//div[@class='watch-view-count']"),LEN(IMPORTXML(B2,"//div[@class='watch-view-count']"))-5) E2: =LEFT(importXML(D2,"//span[@class='fcg']"),LEN(importXML(D2,"//span[@class='fcg']"))-6) F2: =LEFT(SUBSTITUTE(LEFT(E2,LEN(E2)-1),".","")&VLOOKUP(RIGHT(E2,1),viewcounter,2,FALSE),7)
Your spreadsheet should now look something like this:
Step 4: Finishing touches
All we have left to do is adding up our Facebook and Youtube views, and calculating our view per dollar cost. This is the easy part!
I am adding 3 columns to total my views, enter my budget amount, and finally, divide the total views by the budget amount. That’s it, all done!
Notes:
Facebook is weird. You’ll notice pretty often that the data isn’t coming in anymore, even though nothing has changed. Just copy the URLs, delete the content of the cells containing them, and paste them back in. And magic, everything comes back to normal.
It’s probably a good thing to wrap all the formulas in an “IFERROR” functon, to avoid showing “#N/A” when there is no link. You can also wrap the formula in cell G2 in a conditional statement to only add values if they are larger than 0. Otherwise you’ll have a lot of zeros in your document.
18 Comments
Elena · December 7, 2018 at 1:32 pm
Hello Quentin,
This is great and still seems to work ! Would you have a comments number Xpath I can’t seem to get that count up.
Any help would be amazing and save a lot of time !
quentinfr · January 7, 2019 at 8:02 pm
Hey Elena! I don’t have that one 🙁
I was just googling some things to respond to another comment, and found this page which seems to have quite a few handy ressources. Maybe an answer there! https://www.benlcollins.com/spreadsheets/import-social-media-statistics
Jenny Kow · January 6, 2019 at 4:22 am
what about instagram video? is there a way to scrap view count?
quentinfr · January 7, 2019 at 8:03 pm
Hey Jenny! I haven’t dealt with instagram yet. Take a look at this page, I think there might be some good answers:
https://www.benlcollins.com/spreadsheets/import-social-media-statistics/#instagram
Jenny Kow · January 11, 2019 at 5:09 am
Thanks! will check it out
Daniel · January 11, 2019 at 2:43 pm
Hi Quentin, thanks for this. Which would be the xPath to pull the userID or the username rather than the views?
I have been dealing with this but I am not able to find it.
quentinfr · January 12, 2019 at 1:16 am
Hi Daniel, I looked a little into it, but so far I’m in the boat as you. I know that there are a few plugins/softwares out there that simplify the scraping process. That could be the next available optio. Good luck 🙂
Ebra · January 20, 2019 at 5:57 am
Thanks Quentinfr
sounds great, but the facebook xpath seems not working (Facebook Xpath: “//span[@class=’fcg’]”, any suggestion?
Sally · January 22, 2019 at 3:00 am
Hi guys!
the facebook formula doesn’t working return: “#N/A” with such error like: “Imported content is empty”. any advice? additionally any help with instagram and twitter video views?
thanks ^__^
Mikolaj · January 26, 2019 at 2:42 pm
Hi Quentinfr
I would like to thank you very much for these tips! I’ve been looking for something like this for months. YouTube it works, but Facebook not. I try diffrent ways:
=importXML(D2;”//div[@class=’fcg’]”)
=importXML(D2;”//span[@class=’fcg’]”)
=importXML(D2;”//div[@class=’_44bh’]”)
=importXML(D2;”//span[@class=’_44bh’]”)
=importXML(D2;”//div[@class=’fsm fwn fcg’]”)
=importXML(D2;”//span[@class=’fsm fwn fcg’]”)
Do I something wrong? Or maybe Facebook change something recently? Could you help me?
Boissonneault · April 3, 2019 at 9:25 pm
seems like facebook is not working
daniel · April 4, 2019 at 2:00 pm
I could not get it working with facebook. Do you have any suggestions. I am trying to get views and shares of public videos
Judd Spittler · May 6, 2019 at 9:03 pm
I have tested this, and see that it works great for “static” YouTube videos, but I need it to work for Live Streaming YouTube videos, and unfortunately this doesn’t work in that case. Do you happen to have any ideas for getting viewer count for YouTube live streaming videos? Thanks.
Tegan Knight · May 8, 2019 at 1:55 am
Hi Quentin. I’m trying to setup a sheet where I can list YT videos in one column and get the live view count in another. I’m using the formula you provided and have tried others but I keep getting a data error message. Any ideas? (in AF10: https://www.youtube.com/watch?v=idp20v_PTYo // in AG10: =LEFT(IMPORTXML(AF10,”//div[@class=’watch-view-count’]”),LEN(IMPORTXML(AF10,”//div[@class=’watch-view-count’]”))-5)
Dan Shaikh · May 14, 2019 at 5:40 pm
VERY HELPFUL in getting Youtube views and eliminating the view text formulas.
The facebook section didn’t work for me, however I was able to leverage the information https://stackoverflow.com/questions/41253227/how-to-use-importxml-in-google-sheets-to-pull-view-count-data-from-a-facebook-vi
About getting Facebook video views. it was very insightful and worked.
Now I am able to get Youtube Video Views and Facebook video views using just the links…. Very powerful!
The other thing I think is helpful…
I put my XML codes in another sheet and then using the Formula “Value()”
I was able to turn the output into a number to be able to sum my views…
quentinfr · May 14, 2019 at 6:15 pm
Hi everyone,
Facebook is notorious for changing their platform constantly, making it quite difficult to keep up with.
I, unfortunately, haven’t been using this spreadsheet in a long time, so I don’t have the most up to date information. It seems that Dan Shaikh was able to find a workaround though!
Nisam · October 21, 2019 at 5:21 am
Hi,
How can i store view count of youtube live streaming as time series data ?
ปั้มไลค์ · July 21, 2020 at 10:40 pm
Like!! Thank you for publishing this awesome article.