JustAnotherArchivist / snscrape

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

How to access users mentioned in tweets? #248

Closed kiranbhatia16 closed 3 years ago

kiranbhatia16 commented 3 years ago

I am very new to python and I am a little confused. I am working with this script:

keyword= 'bantwitterinindia'
maxTweets = 10
csvFile = open(keyword +'-twitter_scraped-' + '.csv', 'a', newline='', encoding='utf8')

csvWriter = csv.writer(csvFile)
csvWriter.writerow(['id','date','tweet',])

for i,tweet in enumerate(sntwitter.TwitterSearchScraper(keyword + ' lang:en since:' +  '2021-05-01' + ' until:' + '2021-05-31').get_items()):
        if i > maxTweets :
            break
        csvWriter.writerow([tweet.id, tweet.date, tweet.content])
csvFile.close()

I want to include mentioned users in the tweets in this script so that I can have a source and target column for social network analysis. What should I do?

JustAnotherArchivist commented 3 years ago

tweet.mentionedUsers is a list of snscrape.modules.twitter.User objects if there are any mentions in a tweet (None if not). I don't know what your expected structure is for no/one/multiple mentions, but you'll want to add something like this:

if tweet.mentionedUsers:
    for user in tweet.mentionedUsers:
        # Do something with user.username

And as you might suspect, tweet.user is the user that posted the tweet.