简体   繁体   中英

How to extract the text part of hastags using tweepy?

I am trying to scrape some data from twitter to perform sentiments analysis, I want to be able to get the username, tweets, number of likes, number of retweets, location, date and hashtag. Every other one works for me except the hashtag. I am getting both the text and indices, however, I only want the text. Help me, please;( Below is the code for that part;

data = []

for tweet in tweets:
    data.append([tweet.user.screen_name, tweet.full_text, tweet.favorite_count, 
                 tweet.retweet_count, tweet.user.location, tweet.created_at, tweet.entities['hashtags']])
    
df = pd.DataFrame(data, columns = ['Username', 'Tweet', 'No. of Likes', 'No. of Retweets', 'Location', 'Date', 'Hashtag'])
print(df)

I tried tweets.entities['hashtags'] and I expected to get only the hashtags. I even tried tweets['hashtags'][1][1]['text'] since I saw it somewhere but it did not work

to get hashtags as a list use:

list(map(lambda a:a['text'],tweet.entities['hashtags']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM