Wizard Book


Programming Python, Web, Android

Python : Membaca Tweets dengan Tweepy

Membaca Tweets dari Twitter menggunakan Tweepy

import tweepy
import sys

#Twitter Configuration
CONSUMER_KEY    = '4XDW--YOUR-CONSUMER-KEY--ICjO'
CONSUMER_SECRET = '3JpyD--YOUR-CONSUMER-SECRET--bApue'

# Access Configuration:
ACCESS_TOKEN  = '11234--YOUR-ACCESS-TOKEN--Of8ROu'
ACCESS_SECRET = 'brNRkk--YOUR-ACCESS-SECRET--pPGTVwn'


def twitter_init():

    # Authentication and access using keys:
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

    # Return API with authentication:
    api = tweepy.API(auth)
    return api

extractor = twitter_init()

tweets = extractor.search(q="pemilu",lang="id",count="100", tweet_mode='extended')

print("Number of tweets extracted: {}.\n".format(len(tweets)))
#print(tweets)

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

# Empty Array 
tmp=[]
        
tweets_for_csv = [tweet.full_text for tweet in tweets]   
for j in tweets_for_csv: 

    # Appending tweets to the empty array tmp
    tmp.append(j.translate(non_bmp_map))  
        
# Printing the tweets 
print(tmp)

email : contohprogram.com@gmail.com
WA : +6289671400363