處理 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...