我正在设置社交媒体分析,其中包括抓取搜索到的推文列表。现在,我的任务需要从推文中获取提及列表,并将其作为网络可视化的列表。
我正在使用Python 3.7和pandas
数据框来总结我的结果。我在想的一种方法是使用tweet_df['text'].str.contains('[@]\w+', regex=True)
,但没有成功。
import tweepy
import pandas as pd
# Authentication is here, omitted for exhibition purpose
api = tweepy.API(auth)
# My example, will use a crawler later.
tweets = api.search(q = 'state of origin', count = 100, since = '2019-07-05')
print('There are {} tweets have been searched.'.format(len(tweets)))
tweet_df = pd.DataFrame()
for i in range(len(tweets)):
tweet_df = tweet_df.append([[tweets[i].id, tweets[i].text, tweets[i].author.id, tweets[i].author.name, tweets[i].author.location]],
ignore_index = True)
tweet_df.columns = ['tweet_id', 'text', 'author_id', 'author', 'author_location']
print(tweet_df)
# How can I extract the text column and find
tweet_df = tweet_df['text'].str.contains('[@]\w+', regex=True)
我希望会创建一个新的提及专栏,并且会列出诸如pd.DataFrame('mentions':['@user1', '@user2'])
之类的提及清单。