我正在研究tweety模块,我试过这个代码用土耳其语写的推文(土耳其语,有些字符不支持ascii,如ğ,ş,ö,ç,İ,ı)但我需要显然是整个数据。
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
ckey = "****"
csecret = "****"
atoken = "****"
asecret = "****"
class listener(StreamListener):
def on_data(self,data):
tweet = data.split(',"text":"')[1].split('","source')[0]
print tweet
saveThis = str(time.time()) + '::' + tweet
saveData = open("archive.csv","a")
saveData.write(saveThis)
saveData.write("\n")
saveData.close()
return True
def on_error(self,status):
print status
auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream = Stream(auth,listener() )
twitterStream.filter(track = ["galatasaray"])
它给了我
1445282560.38::RT @EndlesGALA: T\u00fcrkiyedir #Galatasaray \nKim daha b\u00fcy\u00fck tart\u0131smayal\u0131m isterseniz http:\/\/t.co\/0K6jLC0CHd
1445282563.02::RT @Gkhanutkan1907: Galatasaray'\u0131 Her Sene Yenmek Bizim \u0130\u00e7in Ba\u015far\u0131 De\u011fil Genlerimizde Olan Bir GELENEKT\u0130R!
1445282563.26::22:22 GALATASARAY
1445282564.84::RT @mthnzncrkrn: Karanl\u0131k Elbet Kavu\u015fur Ayd\u0131nl\u0131\u011fa. Allah yard\u0131mc\u0131n olsun Kadir BABA .\nGALATASARAY taraftar\u0131 hep seninle! #KadirAkta\u015fSu\u00e7suzd\u2026
1445282569.29::RT @EndlesGALA: T\u00fcrkiyedir #Galatasaray \nKim daha b\u00fcy\u00fck tart\u0131smayal\u0131m isterseniz http:\/\/t.co\/0K6jLC0CHd
1445282570.29::Fenerbah\u00e7e - Galatasaray derbisinin biletlerine yo\u011fun ilgi: https:\/\/t.co\/VZ2whsiZNo
1445282571.2::RT @EndlesGALA: T\u00fcrkiyedir #Galatasaray \nKim daha b\u00fcy\u00fck tart\u0131smayal\u0131m isterseniz http:\/\/t.co\/0K6jLC0CHd
1445282571.95::Kom\u015fularla s\u0131f\u0131r sorundan s\u0131f\u0131r kom\u015fuya!! #galatasaray #NTV #yakinda #Bug\u00fcnTV #Ak\u015fam #Takvim #D\u00fcnyaKahveG\u00fcn\u00fc #menzil https:\/\/t.co\/bcHNoG4UMN
我该如何解决?
答案 0 :(得分:2)
tweepy为您提供世界上几乎所有网络服务的JSON编码数据。
tweet = data.split(',"text":"')[1].split('","source')[0]
这不是处理json的正确方法。改为:
import json (at top of script)
然后:
class listener(StreamListener):
def on_data(self,data):
tweet = json.loads(data)
print tweet
tweet将是一个经典的python对象。这是一个字典。