Difference between revisions of "Python: TwitterSearch"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
==Search Twitter== | ==Search Twitter== | ||
− | Contoh search | + | Contoh search dengan beberapa keyword fungsi OR |
− | + | ||
+ | from TwitterSearch import * | ||
− | |||
− | |||
tso = TwitterSearchOrder() # create a TwitterSearchOrder object | tso = TwitterSearchOrder() # create a TwitterSearchOrder object | ||
keywords=("ahok","anies","sylv") | keywords=("ahok","anies","sylv") | ||
tso.set_keywords(keywords, or_operator=True) | tso.set_keywords(keywords, or_operator=True) | ||
− | tso.set_language('id') # | + | tso.set_language('id') # we want to see Indonesian tweets only |
− | tso.set_include_entities( | + | tso.set_include_entities(True) # and don't give us all those entity information |
− | + | ||
# it's about time to create a TwitterSearch object with our secret tokens | # it's about time to create a TwitterSearch object with our secret tokens | ||
ts = TwitterSearch( | ts = TwitterSearch( | ||
− | consumer_key = '', | + | consumer_key = ' ', |
− | consumer_secret = '', | + | consumer_secret = ' ', |
− | access_token = '', | + | access_token = ' ', |
− | access_token_secret = '' | + | access_token_secret = ' ' |
) | ) | ||
# this is where the fun actually starts :) | # this is where the fun actually starts :) | ||
for tweet in ts.search_tweets_iterable(tso): | for tweet in ts.search_tweets_iterable(tso): | ||
− | + | print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'].encode('ascii','ignore').decode('ascii') ) ) | |
+ | |||
Hasilnya kira-kira sebagai berikut, | Hasilnya kira-kira sebagai berikut, | ||
− | @ | + | @suaramasjid tweeted: Inilah Kesaksian Ketum MUI di Persidangan Ahok https://t.co/KLKBLgYu5q |
− | @ | + | @miraclecathy tweeted: Penyakit kanker, jantung, otak itu hrs segera ditangani. Ga ada lg ditolak RS dgn alasan penuh. Ahok peduli itu, sy https://t.co/F2jjtwOkn3 |
− | @ | + | @sofandresyabanu tweeted: Sidang Kemarin, Ahok Tidak Bertujuan Laporkan Saksi Maruf Amin, Tapi Mau Laporkan Habib Muchsin https://t.co/B2UISB8xwg |
− | @ | + | @ugisugiharto tweeted: RT @elpanjullo: https://t.co/EwE2J6YFVw Ayeeeee dukung artikel bagus ini... Bebaskan Ahok Demi Keadilan! Ayeeeee 100% yakin Ahok Kagak Sala |
− | @ | + | @bangher_id tweeted: RT pks_cakung: Emak3 siap mengantarkan Anies Sandi menjadi gubernur DKI #AniesSandiIdamanRakyat PKSJakarta JktMaju https://t.co/jIvPcQGgSy |
− | @ | + | @atopbbx tweeted: Metro_TV: [Populer] Jubir Ahok: Megawati Bakal Turun di Saat yang Tepat https://t.co/d9ekkDaCF7 https://t.co/xiKzYanGZF |
− | @ | + | @ssirah tweeted: RT @nay_shihab: KH Didin Hafidhuddin: Ahok Membahayakan |
+ | https://t.co/M65Fkx0es8 | ||
==Access User Timelines== | ==Access User Timelines== | ||
Line 54: | Line 55: | ||
# it's about time to create a TwitterSearch object with our secret tokens | # it's about time to create a TwitterSearch object with our secret tokens | ||
ts = TwitterSearch( | ts = TwitterSearch( | ||
− | consumer_key = '', | + | consumer_key = ' ', |
− | consumer_secret = '', | + | consumer_secret = ' ', |
− | access_token = '', | + | access_token = ' ', |
− | access_token_secret = '' | + | access_token_secret = ' ' |
) | ) | ||
Line 71: | Line 72: | ||
@onnowpurbo tweeted: openwrt dulunya utk router, cuma dengan kemampuan yang ada bisa utk macem2 :) .. https://t.co/3VZgng1RLd | @onnowpurbo tweeted: openwrt dulunya utk router, cuma dengan kemampuan yang ada bisa utk macem2 :) .. https://t.co/3VZgng1RLd | ||
@onnowpurbo tweeted: bisa diskusi via email onno@indo.net.id, beraba besar komunitas pulpstone? https://t.co/GEksxU49Zy | @onnowpurbo tweeted: bisa diskusi via email onno@indo.net.id, beraba besar komunitas pulpstone? https://t.co/GEksxU49Zy | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Referensi== | ==Referensi== | ||
Line 98: | Line 77: | ||
* https://pypi.python.org/pypi/TwitterSearch/ | * https://pypi.python.org/pypi/TwitterSearch/ | ||
* https://github.com/ckoepp/TwitterSearch/tree/master/docs | * https://github.com/ckoepp/TwitterSearch/tree/master/docs | ||
+ | * https://github.com/ckoepp/TwitterSearch/blob/master/tests/test_tso.py |
Latest revision as of 12:02, 2 February 2017
Instalasi
pip install TwitterSearch
atau
easy_install TwitterSearch
Search Twitter
Contoh search dengan beberapa keyword fungsi OR
from TwitterSearch import *
tso = TwitterSearchOrder() # create a TwitterSearchOrder object keywords=("ahok","anies","sylv") tso.set_keywords(keywords, or_operator=True) tso.set_language('id') # we want to see Indonesian tweets only tso.set_include_entities(True) # and don't give us all those entity information # it's about time to create a TwitterSearch object with our secret tokens ts = TwitterSearch( consumer_key = ' ', consumer_secret = ' ', access_token = ' ', access_token_secret = ' ' ) # this is where the fun actually starts :) for tweet in ts.search_tweets_iterable(tso): print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'].encode('ascii','ignore').decode('ascii') ) )
Hasilnya kira-kira sebagai berikut,
@suaramasjid tweeted: Inilah Kesaksian Ketum MUI di Persidangan Ahok https://t.co/KLKBLgYu5q @miraclecathy tweeted: Penyakit kanker, jantung, otak itu hrs segera ditangani. Ga ada lg ditolak RS dgn alasan penuh. Ahok peduli itu, sy https://t.co/F2jjtwOkn3 @sofandresyabanu tweeted: Sidang Kemarin, Ahok Tidak Bertujuan Laporkan Saksi Maruf Amin, Tapi Mau Laporkan Habib Muchsin https://t.co/B2UISB8xwg @ugisugiharto tweeted: RT @elpanjullo: https://t.co/EwE2J6YFVw Ayeeeee dukung artikel bagus ini... Bebaskan Ahok Demi Keadilan! Ayeeeee 100% yakin Ahok Kagak Sala @bangher_id tweeted: RT pks_cakung: Emak3 siap mengantarkan Anies Sandi menjadi gubernur DKI #AniesSandiIdamanRakyat PKSJakarta JktMaju https://t.co/jIvPcQGgSy @atopbbx tweeted: Metro_TV: [Populer] Jubir Ahok: Megawati Bakal Turun di Saat yang Tepat https://t.co/d9ekkDaCF7 https://t.co/xiKzYanGZF @ssirah tweeted: RT @nay_shihab: KH Didin Hafidhuddin: Ahok Membahayakan
Access User Timelines
Alternatif lain adalah mengakses timeline seorang user saja, caranya,
from TwitterSearch import * tuo = TwitterUserOrder('onnowpurbo') # create a TwitterUserOrder # it's about time to create a TwitterSearch object with our secret tokens ts = TwitterSearch( consumer_key = ' ', consumer_secret = ' ', access_token = ' ', access_token_secret = ' ' ) # start asking Twitter about the timeline for tweet in ts.search_tweets_iterable(tuo): print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) )
Hasilnya kira-kira,
@onnowpurbo tweeted: alhamdulillah :) ... https://t.co/JRQotgThNF @onnowpurbo tweeted: keren! ... https://t.co/xuCqpVJMAd @onnowpurbo tweeted: sama2 lihat aktifitas di pulpstone kayanya menarik nih .. https://t.co/PN3nnf0ztW @onnowpurbo tweeted: openwrt dulunya utk router, cuma dengan kemampuan yang ada bisa utk macem2 :) .. https://t.co/3VZgng1RLd @onnowpurbo tweeted: bisa diskusi via email onno@indo.net.id, beraba besar komunitas pulpstone? https://t.co/GEksxU49Zy