2017-07-12 3 views
1

Je cherche tweets de blockchain en utilisant tweepy, pour que j'utilise le code suivant:Parse réponse tweepy

Auth:

consumerKey = "" 
consumerSecret = "" 
accessToken = "" 
accessTokenSecret = "" 

auth = tweepy.OAuthHandler(consumerKey, consumerSecret) 
auth.set_access_token(accessToken, accessTokenSecret) 
api = tweepy.API(auth) 

Recherche:

query = 'blockchain' 
max_tweets = 5 
searched_tweets = [status for status in tweepy.Cursor(api.search, q=query).items(max_tweets)] 

Mais Je ne suis pas clair sur la façon d'analyser les données à partir d'ici, la variable searched_tweets est une liste avec un seul élément:

print type(searched_tweets) 
print len(searched_tweets) 

<type 'list'> 
5 

Le premier élément à partir de là est un tweepy.models.Status qui ne parvient pas à analyser.

"Status(contributors=None, truncated=False, text=u'RT @AdEx_Network: A shoutout to Richard Kastelein for mentioning the AdEx crowdsale #ico #tokensale #ad\u2026', is_quote_status=False, in_reply_to_status_id=None, id=884958221147938816L, favorite_count=0, _api=<tweepy.api.API object at 0xb25ec08c>, author=User(follow_request_sent=False, has_extended_profile=False, profile_use_background_image=True, _json={u'follow_request_sent': False, u'has_extended_profile': False, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 846284378112090112L, u'profile_background_image_url_https': None, u'verified': False, u'translator_type': u'none', u'profile_text_color': u'333333', u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/873212431106834432/-QdMHr0M_normal.jpg', u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 735, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'846284378112090112', u'profile_background_color': u'F5F8FA', u'listed_count': 0, u'is_translation_enabled': False, u'utc_offset': None, u'statuses_count': 100, u'description': u'', u'friends_count': 126, u'location': u'', u'profile_link_color': u'1DA1F2', u'profile_image_url': u'http://pbs.twimg.com/profile_images/873212431106834432/-QdMHr0M_normal.jpg', u'following': False, u'geo_enabled': False, u'profile_background_image_url': None, u'screen_name': u'AfiniChristy', u'lang': u'id', u'profile_background_tile': False, u'favourites_count': 100, u'name': u'Fitri Christy Afini', u'notifications'" 

Répondre

1

Pour obtenir le texte de l'objet Etat, vous avez deux options. La première est d'utiliser simplement .text et obtenir le texte des tweets dans la liste, ainsi, par exemple:

>>> searched_tweets[0].text 
'RT @LiquidHub: The World Of #Cryptocurrency\n\n#Blockchain #Fintech #makeyourownlane #Mpgvip #AI #defstar5 #IOT #Bitcoin #GrowthHacking #Bigd…' 

Avis ce tronque avec « ... » à la fin du tweet, par les docs ce est parce qu'ils sont en fait retweets, pour obtenir un texte intégral d'un objet d'état, vous devez modifier votre demande et puis utilisez .full_text:

searched_tweets = [status for status in tweepy.Cursor(api.search, q=query, tweet_mode='extended').items(max_tweets)] 

# Then you can print the full text tweets: 
>>> searched_tweets[4].full_text 
'RT @StakepoolCom: Crypto Percentages Up Over 100% Today Check it out! #cryptocurrency #steem #blockchain'