JustAnotherArchivist / snscrape

A social networking service scraper in Python
GNU General Public License v3.0
4.5k stars 713 forks source link

How to get number of likes, retweets, followers, and friends for a tweet? #138

Closed Mia-prg closed 4 years ago

Mia-prg commented 4 years ago

Hello,

I tried to get following Old Twitter data. But my code is not working well. This code works when I remove "tweet.favorite_count" and "tweet.retweet_count".

How can I get following data? ・tweet date(◯) ・tweet user name(◯) ・tweet text(◯) ・Number of likes(i cannot get ✕) ・Number of retweets(✕) ・Number of followers(✕) ・Number of friends(✕)

CODE:

col =["tweet_id","tweet.date","tweet.username","tweet.content","tweet.favorite_count","tweet.retweet_count"]
df = pd.DataFrame(columns=col)
df

maxTweets = 10  # the number of tweets you require
keyword = "天気の子"
date = 'since:2019-07-01 until:2019-07-31 min_faves:1 exclude:retweets'

for i,tweet in enumerate(sntwitter.TwitterSearchScraper(keyword + date).get_items()) :
        if i > maxTweets :
            break
        tmp = pd.Series([tweet.id, tweet.date, tweet.username, tweet.content, tweet.favorite_count, tweet.retweet_count], index=df.columns)
        df = df.append( tmp, ignore_index=True )

df

ERROR RESULT:

AttributeError                            Traceback (most recent call last)
<ipython-input-25-1a917510e65d> in <module>()
     10         if i > maxTweets :
     11             break
---> 12         tmp = pd.Series([tweet.id, tweet.date, tweet.username, tweet.content, tweet.favorite_count, tweet.retweet_count], index=df.columns)
     13         df = df.append( tmp, ignore_index=True )
     14 

AttributeError: 'Tweet' object has no attribute 'favorite_count'

I tried to change from "tweet.favorite_count" to "tweet.likeCount" etc, but it doesn't work anyways😢

Could you give me some advice? Please and Thank you.

JustAnotherArchivist commented 4 years ago

(I also fixed your formatting.)

Mia-prg commented 4 years ago

Thank you so much for your kind advice!! It helps me a lot!