skip to Main Content

I used the Twitter Premium API to get historical tweets (Python with the searchtweets package).

I exported the stream to a text file but I don’t know how to structure and analyse the data.

I want to conduct a basic content analysis. Thanks

from searchtweets import ResultStream, gen_rule_payload, load_credentials

premium_search_args = load_credentials("~/.twitter_keys.yaml",
                                       yaml_key="search_tweets_api",
                                       env_overwrite=False)

rule = gen_rule_payload("from:ManonMasse_Qs",
                        from_date="2018-08-23",
                        to_date="2018-10-01",
                        results_per_call=100)

print(rule)

rs = ResultStream(rule_payload=rule,
                  max_results=100,
                  max_pages=1,
                  **premium_search_args)

print(rs)

tweets = list(rs.stream())

[print(tweet.all_text) for tweet in tweets[0:10]];

with open('listfile.txt', 'w') as filehandle:
    filehandle.writelines("%sn" % place for place in tweets)

f=open("Tweets Manon.txt", "r")

contents =f.read()

I tried this code but I get a NameError:

> for line in fileinput.FileInput("Tweets Manon.txt"):
>     try:
>         tweet_dict = json.loads(line)
>         tweet = Tweet(tweet_dict)
>     except (json.JSONDecodeError,NotATweetError):
>         pass
>     print(tweet.created_at_string, tweet.all_text)
> 

>      Traceback (most recent call last):   File "<pyshell#54>", line 7, in <module>
>     print(tweet.created_at_string, tweet.all_text) NameError: name 'tweet' is not defined

2

Answers


  1. From your code snippet, you declare the variable tweet inside your try block, thus getting the name error when trying to access tweet in the print statement outside your try block.

    Login or Signup to reply.
  2. for line in fileinput.FileInput("Tweets Manon.txt"):
        try:
            tweet_dict = json.loads(line)
            tweet = Tweet(tweet_dict)
            print(tweet.created_at_string, tweet.all_text)
        except (json.JSONDecodeError,NotATweetError):
            pass
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search