0

I'm currently using Twitter API to search for tweets using tweepy, and I'm trying to parse for specific parts of a tweet like created_at, favorites, retweets, user id, id, and text of the tweet, but when I run my code, nothing happens?

So far I've been able to obtain the data from twitter and put it in json format, I have a text file with that data, so when I call it, it's supposed to iterate through that file and pull the specific items for each tweet.

Here is the code for what i'm trying to attempt, parsing wise.

filename = "clipboard2.txt"
jsonfile = open(filename, "r")
for line in jsonfile:
try:
    rows = json.loads(line.strip())
    if 'text' in rows:  # only messages contains 'text' field is a tweet
        print("rows"['id'])  # This is the tweet's id
        print(["rows"]['created_at'])  # when the tweet posted
        print(rows["rows"]['text']) # content of the tweet
        print(rows["rows"]['retweet_count'])
        print(rows["rows"]['favorite_count'])
        print["rows"]["user-id"]  # id of the user who posted the tweet
        print(rows["rows"]['user']['name'])  # name of the user, e.g. "Wei Xu"
        print(rows["rows"]['user']['screen_name'])
except:
    continue

My text file/code looks like this: Text file

It seems like there's no error, since it runs with no errors, but I'm not too sure how to handle the data. The way that I'm thinking is that I have "rows" in quotations because that's the first line of the json file and everything following it is stored in an array, correct? So what I want is it to iterate over each part of the array with the items that I have and print out every element of the tweet that has those.

0

The following code works for me.

IMPORT NECESSARY PACKAGES

from tweet_parser.tweet import Tweet
from tweet_parser.tweet_parser_errors import NotATweetError

PARSE

filename = "tweet_data.txt"
jsonfile = open(filename, "r")
for line in jsonfile:    
    try:
        if not line.strip(): continue # skips the empty lines, if any
        tweet_dict = json.loads(line)
        tweet = Tweet(tweet_dict)
    except:
        pass
    if tweet.text != None:
        print(tweet.id)  # This is the tweet's id
        print(tweet.created_at_datetime)  # when the tweet posted
        print(tweet.text) # content of the tweet
        print(tweet.retweet_count)
        print(tweet.favorite_count)
        print(tweet.user_id)
        print(tweet.screen_name)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.