开发者

return actual tweets in tweepy?

I was writing a twitter program using tweepy. When I run this code, it prints the Python ... values for them, like

<tweepy.models.Status object at 0x95ff8cc开发者_如何学Go>

Which is not good. How do I get the actual tweet?

import tweepy, tweepy.api
key = XXXXX
sec = XXXXX

tok  = XXXXX
tsec = XXXXX

auth = tweepy.OAuthHandler(key, sec)
auth.set_access_token(tok, tsec)
api = tweepy.API(auth)

pub = api.home_timeline()
for i in pub:
        print str(i)


In general, you can use the dir() builtin in Python to inspect an object.

It would seem the Tweepy documentation is very lacking here, but I would imagine the Status objects mirror the structure of Twitter's REST status format, see (for example) https://dev.twitter.com/docs/api/1/get/statuses/home_timeline

So -- try

print dir(status)

to see what lives in the status object

or just, say,

print status.text
print status.user.screen_name


Have a look at the getstate() get method which can be used to inspect the returned object

for i in pub:
    print i.__getstate__()


The api.home_timeline() method returns a list of 20 tweepy.models.Status objects which correspond to the top 20 tweets. That is, each Tweet is considered as an object of Status class. Each Status object has a number of attributes like id, text, user, place, created_at, etc.

The following code would print the tweet id and the text :

tweets = api.home_timeline()
for tweet in tweets:
  print tweet.id, " : ", tweet.text


from actual tweets,if u want specific tweet,u must have a tweet id, and use

tweets = self.api.statuses_lookup(tweetIDs)
for tweet in tweets:
  #tweet obtained
  print(str(tweet['id'])+str(tweet['text']))

or if u want tweets in general use twitter stream api

class StdOutListener(StreamListener):
def __init__(self, outputDatabaseName, collectionName):
    try:
        print("Connecting to database")
        conn=pymongo.MongoClient()
        outputDB = conn[outputDatabaseName]
        self.collection = outputDB[collectionName]
        self.counter = 0
    except pymongo.errors.ConnectionFailure as e:
        print ("Could not connect to MongoDB:")
def on_data(self,data): 
    datajson=json.loads(data)
    if "lang" in datajson and datajson["lang"] == "en" and "text" in datajson:
        self.collection.insert(datajson)

        text=datajson["text"].encode("utf-8") #The text of the tweet
        self.counter += 1
        print(str(self.counter) + " " +str(text))

def on_error(self, status):
    print("ERROR")
    print(status)
def on_connect(self):
    print("You're connected to the streaming server.
l=StdOutListener(dbname,cname)
    auth=OAuthHandler(Auth.consumer_key,Auth.consumer_secret)
    auth.set_access_token(Auth.access_token,Auth.access_token_secret)
    stream=Stream(auth,l)


    stream.filter(track=stopWords)

create a class Stdoutlistener which is inherited from StreamListener override function on_data,and tweet is returned in json format,this function runs every time tweet is obtained tweets are filtered accrding to stopwords which is list of u words u wants in ur tweets


On a tweepy Status instance you can can access the _json attribute, which returns a dict representing the original Tweet contents.

For example:

type(status)
# tweepy.models.Status

type(status._json)
# dict

status._json.keys()
# dict_keys(['favorite_count', 'contributors', 'id', 'user', ...])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜