Home > Software engineering >  Want to get twitter data using tweepy but in trouble
Want to get twitter data using tweepy but in trouble

Time:03-30

I am trying to retrieve Twitter data using Tweepy, using that below code, but I'm having difficulties in collecting media_fields data. Especially, I want to get the type of media, but I failed. As you can see below, the value is copied and exists in the cell that should be empty.

[enter image description here][1]

import tweepy
from twitter_authentication import bearer_token
import time
import pandas as pd

client = tweepy.Client(bearer_token, wait_on_rate_limit=True)

hoax_tweets = []
for response in tweepy.Paginator(client.search_all_tweets, 
                                 query = 'Covid hoax -is:retweet lang:en',
                                 user_fields = ['username', 'public_metrics', 'description', 'location','verified','entities'],
                                 tweet_fields=['id', 'in_reply_to_user_id', 'referenced_tweets', 'context_annotations',
                                               'source', 'created_at', 'entities', 'geo', 'withheld', 'public_metrics',
                                              'text'],
                                 media_fields=['media_key', 'type', 'url', 'alt_text', 
                                               'public_metrics','preview_image_url'], 
                                 expansions=['author_id', 'in_reply_to_user_id', 'geo.place_id', 
                                             'attachments.media_keys','referenced_tweets.id','referenced_tweets.id.author_id'],
                                 place_fields=['id', 'name', 'country_code', 'place_type', 'full_name', 'country',
                                               'geo', 'contained_within'],
                                 start_time = '2021-01-20T00:00:00Z',
                                 end_time = '2021-01-21T00:00:00Z',
                              max_results=100):
    time.sleep(1)
    hoax_tweets.append(response)
result = []
user_dict = {}
media_dict = {}


# Loop through each response object
for response in hoax_tweets:
    # Take all of the users, and put them into a dictionary of dictionaries with the info we want to keep
    for user in response.includes['users']:
        user_dict[user.id] = {'username': user.username, 
                              'followers': user.public_metrics['followers_count'],
                              'tweets': user.public_metrics['tweet_count'],
                              'description': user.description,
                              'location': user.location,
                              'verified': user.verified
                             }
        for media in response.includes['media']:
            media_dict[tweet.id] = {'media_key':media.media_key,
                            'type':media.type            
                            }

    for tweet in response.data:
        # For each tweet, find the author's information
        author_info = user_dict[tweet.author_id]
        # Put all of the information we want to keep in a single dictionary for each tweet
        result.append({'author_id': tweet.author_id, 
                       'username': author_info['username'],
                       'author_followers': author_info['followers'],
                       'author_tweets': author_info['tweets'],
                       'author_description': author_info['description'],
                       'author_location': author_info['location'],
                       'author_verified':author_info['verified'],
                       'tweet_id': tweet.id,
                       'text': tweet.text,
                       'created_at': tweet.created_at,
                       'retweets': tweet.public_metrics['retweet_count'],
                       'replies': tweet.public_metrics['reply_count'],
                       'likes': tweet.public_metrics['like_count'],
                       'quote_count': tweet.public_metrics['quote_count'],
                       'in_reply_to_user_id':tweet.in_reply_to_user_id,
                       'media':tweet.attachments,
                       'media_type': media,
                       'conversation':tweet.referenced_tweets
                      })

# Change this list of dictionaries into a dataframe
df = pd.DataFrame(result)

Also, when I change the code ''media':tweet.attachments' to 'media':tweet.attachments[0] to get 'media_key' data, I get the following error message."TypeError: 'NoneType' object is not subscriptable"

What am I doing wrong? Any suggestions would be appreciated. [1]: https://i.stack.imgur.com/AxCcl.png

CodePudding user response:

The subscriptable error comes from the fact that tweet.attachments is None, from here the NoneType part. To make it work, you can add a check for None:

'media':tweet.attachments[0] if tweet.attachments else None 

I have never used the twitter API, but one thing is to make sure the tweet attachments are always present or if they may be absent.

  • Related