Advertisement
Scriptomania_

Free Twitter Followers Scraper

Aug 25th, 2011
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. ###########################################
  2. #Twitter followers scraper by Scriptomania#
  3. ###########################################
  4. # code is available freely, can be reused,
  5. # forked, used commercially and whatever...
  6. # no attribution to original author needed
  7. #
  8. #
  9. # Version 1.0
  10. # Current limitations:
  11. # - 150 requests per hour (due to API use)
  12. # - max 5k IDs per user (will be fixed)
  13. #
  14. #
  15. # download the simplejson lib at:
  16. # http://pypi.python.org/pypi/simplejson/
  17.  
  18. import httplib, simplejson, sys, urllib2
  19.  
  20. # Get initial set of ids
  21. oUser = raw_input("Enter a Twitter username to scrape from: ")
  22. oFileName = raw_input("Enter filename to write to (without extension): ")
  23. if oUser == "":
  24.     exit()
  25. if oFileName == "":
  26.     exit()
  27. print "Getting user ids"
  28. hOpen = httplib.HTTPSConnection("api.twitter.com")
  29. hOpen.request("GET", "/1/followers/ids.json?cursor=-1&screen_name=" + oUser)
  30. hResponse = hOpen.getresponse()
  31. Buffer = hResponse.read()
  32. oJsonString = simplejson.loads(Buffer)
  33.  
  34. # Define a group
  35. def group(lst, n):
  36.     for i in range(0, len(lst), n):
  37.         val = lst[i:i+n]
  38.         if len(val) == n:
  39.             yield tuple(val)
  40.  
  41. # Take a batch of 100, lookup and save to file            
  42. for followers_list in group(oJsonString['ids'], 100):
  43.     hOpen.request("GET", '/1/users/lookup.json?user_id=%s' % (urllib2.quote(','.join(map(str, followers_list)))))
  44.     try:
  45.         hResponse = hOpen.getresponse()
  46.         Buffer = hResponse.read()
  47.         details_json = simplejson.loads(Buffer)
  48.         f = open(oFileName + ".txt", "a")
  49.         for detail in details_json:
  50.             print detail['screen_name']
  51.             f.write(detail['screen_name'] + "\n")
  52.         f.close()
  53.     except httplib.CannotSendRequest, e:
  54.         pass
  55.     except httplib.BadStatusLine, e:
  56.         pass
  57.     except httplib.HTTPException, e:
  58.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement