Search code examples
pythonlistsearchtwitter

find users on twitter and save them as a list


I am a beginner in python, here is what I did and want to do with python: I searched for a hashtag in tweeter and saved the users that used the hashtag, now my question is how I can save these users as a list, since I want to find followings of these users later. Here is my code:

for i in tweepy.Cursor(api.search, q="#hashtag").items():
    author = i.author.id
    tweet = i.text.replace('\n',' ').replace('\r',' ').replace('\r\n',' ')

Thank you!


Solution

  • For this, you can use a set https://docs.python.org/3.6/library/stdtypes.html#set. You can use something like this:

    hashtag_search_results = tweepy.Cursor(api.search, q="#hashtag")
    authors = set()
    
    for i in hashtag_search_results.items():
        author = i.author.id
        tweet = i.text.replace('\n',' ').replace('\r',' ').replace('\r\n',' ')
    
        authors.add(author)
        if len(authors) == 100:
            break
    
    # work with the authors set