Advertisement
zbeucler

Twitter API Scrapper

Apr 10th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #Zack Beucler
  2. #https://github.com/ckoepp/TwitterSearch
  3. #https://pypi.org/project/TwitterSearch/
  4.  
  5. '''
  6. XXXXXXXXXXXXXXXX READ ME XXXXXXXXXXXXXXXX
  7. - install required packages
  8.  - pip install TwitterSearch
  9. - Make a developer Account with Twitter (developer.twitter.com) and get your:
  10.    1. consumer_key
  11.    2. consumer_secret
  12.    3. access_token
  13.    4. access_token_secret
  14.  - Input your four keys ^^^^ into line 36-39 as strings
  15. '''
  16.  
  17.  
  18. from TwitterSearch import *
  19.  
  20.  
  21.  
  22. def runSearch(keyword):
  23.   print("-----------------------------------")
  24.  
  25.   while keyword == "EXIT":
  26.     print("Bye!")
  27.     return
  28.  
  29.   try:
  30.       tso = TwitterSearchOrder() # create a TwitterSearchOrder object
  31.       tso.set_keywords([keyword]) # let's define all words we would like to have a look for
  32.       tso.set_language('en') # we want to see english tweets only
  33.       tso.set_include_entities(False) # and don't give us all those entity information
  34.  
  35.       # it's about time to create a TwitterSearch object with our secret tokens
  36.       ts = TwitterSearch(
  37.           consumer_key = 'consumer_key',
  38.           consumer_secret = 'consumer_secret',
  39.           access_token = 'access_token',
  40.           access_token_secret = 'access_token_secret'
  41.       )
  42.      
  43.  
  44.       # this is where the API data is collected and sorted for printing
  45.       count = 0
  46.       for tweet in ts.search_tweets_iterable(tso):
  47.         if count == 250:
  48.           print("250 Tweets ==> Max Limit Reached, Program stopping")
  49.           return
  50.         else:
  51.           print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) )
  52.           count = count + 1
  53.           print()
  54.           print("#################################################")
  55.           print()
  56.        
  57.       print(count, " Total tweets")
  58.  
  59.  
  60.   except TwitterSearchException as e: # take care of all those ugly errors if there are some
  61.       print(e)
  62.  
  63.  
  64. ########################################################################################################
  65.  
  66.  
  67.  
  68. def main():
  69.   keyword = ""
  70.   while keyword != "EXIT":
  71.     print()
  72.     print("TWITTER POST SCRAPPER")
  73.     print("### WARNING ###")
  74.     print("### Popular keywords (EX: IDK) will bring up a fuck ton of results, press control+c to stop the program ### ")
  75.     keyword = input("Please enter a keyword (Type EXIT to leave program): ")
  76.     runSearch(keyword)
  77. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement