Search code examples
pythonstringoutputtweepytweets

How do I place multiple searched tweets into string


I have a program set up so it searches tweets based on the hashtag I give it and I can edit how many tweets to search and display but I can't figure out how to place the searched tweets into a string. this is the code I have so far

    while True:
        for status in tweepy.Cursor(api.search, q=hashtag).items(2):
                tweet = [status.text]
print tweet

when this is run it only outputs 1 tweet when it is set to search 2


Solution

  • Your code looks like there's nothing to break out of the while loop. One method that comes to mind is to set a variable to an empty list and then with each tweet, append that to the list.

    foo = []
    for status in tweepy.Cursor(api.search, q=hashtag).items(2):
       tweet = status.text
       foo.append(tweet)
    print foo
    

    Of course, this will print a list. If you want a string instead, use the string join() method. Adjust the last line of code to look like this:

    bar = ' '.join(foo)
    print bar