<jupyter_start><jupyter_text>Twitter - Get tweets from search **Tags:** twitter automation ifttt naas_drivers  Input  Import libraries<jupyter_code>import tweepy
import pandas as pd<jupyter_output><empty_output><jupyter_text>API Credentials<jupyter_code>consumer_key = "<KEY>"
consumer_secret = "<KEY>"<jupyter_output><empty_output><jupyter_text>How to generate API Keys? [Twitter API Documentation](https://developer.twitter.com/en/docs/getting-started)  Model  Authentification<jupyter_code>try:
    auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
    api = tweepy.API(auth)
except BaseException as e:
    print(f"Authentication has been failed due to -{str(e)}")<jupyter_output><empty_output><jupyter_text>Fonctions<jupyter_code>def getTweets(search_words, date_since, numTweets):
    
    # Define a pandas dataframe to store the date:
    tweets_df = pd.DataFrame(columns = ['username', 'desc', 'location', 'following',
                                        'followers', 'totaltweets', 'usercreated', 'tweetcreated',
                                        'retweetcount', 'text', 'hashtags']
                                )

    # Collect tweets using the Cursor object
    # .Cursor() returns an object that you can iterate or loop over to access the data collected.
    tweets = tweepy.Cursor(api.search, q=search_words, lang="en", since=date_since, tweet_mode='extended').items(numTweets)
    # Store tweets into a python list
    tweet_list = [tweet for tweet in tweets]
    for tweet in tweet_list:
        username = tweet.user.screen_name
        desc = tweet.user.description
        location = tweet.user.location
        following = tweet.user.friends_count
        followers = tweet.user.followers_count
        totaltweets = tweet.user.statuses_count
        usercreated = tweet.user.created_at
        tweetcreated = tweet.created_at
        retweetcount = tweet.retweet_count
        hashtags = tweet.entities['hashtags']
        try:
            text = tweet.retweeted_status.full_text
        except AttributeError:
            text = tweet.full_text
        
        tweet_data = [username, desc, location, following, followers, totaltweets,
                        usercreated, tweetcreated, retweetcount, text, hashtags]
        
        tweets_df.loc[len(tweets_df)] = tweet_data
        
    
    return tweets_df<jupyter_output><empty_output><jupyter_text>Initialise these function attributes:<jupyter_code>search_words = "#jupyterlab OR #python OR #naas OR #naasai"
date_since = "2021-09-21"
numTweets = 50<jupyter_output><empty_output><jupyter_text>Output  Get the tweets<jupyter_code>df = getTweets(search_words, date_since, numTweets)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09328908554572271, 'fraction_numerical': 0.003687315634218289, 'mean_word_length': 3.4185667752442996, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '262491', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 878, 'n_tokens_neox': 829, 'n_words': 178, 'path': 'Twitter/Twitter_Get_tweets_from_search.ipynb', 'repo_name': 'Charles-de-Montigny/awesome-notebooks', 'size': '2756'}