处理 Twitter 文本

在 Python 中分析社交媒体数据

Alex Hanna

Computational Social Scientist

Twitter JSON 中的文本

tweet_json = open('tweet-example.json', 'r').read()
tweet = json.loads(tweet_json)
tweet['text']
在 Python 中分析社交媒体数据

超过 140 个字符

tweet['extended_tweet']['full_text']
在 Python 中分析社交媒体数据

转推与引用推文

tweet['quoted_status']['extended_tweet']['full_text']
在 Python 中分析社交媒体数据

用户文本信息

tweet['user']['description']
tweet['user']['location']
在 Python 中分析社交媒体数据

扁平化 Twitter JSON

extended_tweet['extended_tweet-full_text'] = 
    extended_tweet['extended_tweet']['full_text']
在 Python 中分析社交媒体数据

扁平化 Twitter JSON

tweet_list = []
with open('all_tweets.json', 'r') as fh:
    tweets_json = fh.read().split("\n")

for tweet in tweets_json: tweet_obj = json.loads(tweet)
if 'extended_tweet' in tweet_obj: tweet_obj['extended_tweet-full_text'] = tweet_obj['extended_tweet']['full_text'] ...
tweet_list.append(tweet)
tweets = pd.DataFrame(tweet_list)
在 Python 中分析社交媒体数据

让我们来练习!

在 Python 中分析社交媒体数据

Preparing Video For Download...