Loading and accessing tweets
In the video, we loaded a tweet we collected using tweepy
into Python. Tweets arrive from the Streaming API in JSON format and need to be converted into a Python data structure.
In this exercise, we'll load a single tweet into Python and print out some fields.
The tweet JSON has been loaded for you and is stored in tweet_json
.
This exercise is part of the course
Analyzing Social Media Data in Python
Exercise instructions
- Import the
json
module. - Convert the tweet JSON stored in
tweet_json
from JSON to Python object usingjson
's.loads()
method. - Print the tweet text and id using the appropriate keys.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load JSON
import ____
# Convert from JSON to Python object
tweet = ____.____(tweet_json)
# Print tweet text
print(tweet[____])
# Print tweet id
print(tweet[____])