Head of Digital Marketing @Bronco. Lover of Python and Data.

Scraping Twitter And Facebook Shares With Python

There are obviously dozens of reasons to want to see Twitter and FaceBook shares, so I have written a surprisingly simple script to do the job for me - I thought this would go into 50+ lines of code at least. Obviously this is only checking against one URL but a slight modification and I could feed in multiple URLs of competitors, a news or blog section or an entire site.

It's using the share apis and it parses the json data to display what we're after. You can go here to see the original data that is fetched - http://cdn.api.twitter.com/1/urls/count.json?url=http://www.craigaddyman.com/an-interview-with-rand-fishkin-of-seomoz/

As you can see the following is displayed :

{"count":31,"url":"http:\/\/www.craigaddyman.com\/an-interview-with-rand-fishkin-of-seomoz\/"}

Its the "Count":31 section we are after. Anyway the code and output are below!!


import requests
import json

url = "http://www.craigaddyman.com/an-interview-with-rand-fishkin-of-seomoz/"
apis = ['http://graph.facebook.com/?id=', 'http://cdn.api.twitter.com/1/urls/count.json?url=']

# http://graph.facebook.com/?id= # facebook shares api
# http://cdn.api.twitter.com/1/urls/count.json?url= # twitter shares api

for api in apis:
    r = requests.get(api + url)
    data = r.text
    x = json.loads(data) # now acts as a dict
    if 'shares' in x:
        try:
            print 'FaceBook shares', x['shares']
        except:
            pass
    elif 'count' in x:
        try:
            print 'Twitter shares', x['count']
        except:
            pass

Output...


adders@adders ~/Desktop/crawler $ python my_spider.py
FaceBook shares 9
Twitter shares 31


Give Your Inbox Some Love


What You'll get?
  • Posts like this delivered straight to your inbox!
  • Nothing else.
Comment Policy

Any code snippets more than a line or 2, please include as a link to a gist

comments powered by Disqus