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.