'Tweepy connection error: host has forcbily removed the connection

# OAuth2.0 Version 
import tweepy as tw
import time
import pandas as pd

#Put your Bearer Token in the parenthesis below
client = tw.Client(bearer_token='removedfromcode',
                      wait_on_rate_limit=True)

# -is:retweet means I don't want retweets
# lang:en is asking for the tweets to be in english

query = 'Manchester United Man United Man Utd lang:en -is:retweet'


ManUtd_tweets = []
ManUtd_tweets_ids = []


while True:
    try:
        for tweet in tw.Paginator(client.search_all_tweets, query=query,
                          user_fields = ['username','public_metrics','description', 'location'],
                          tweet_fields=['text', 'created_at','public_metrics'],
                          start_time = '2021-01-01T00:00:00Z',
                          end_time = '2022-01-01T00:00:00Z', max_results=100) \
                          .flatten(limit=1000000):

            
            ManUtd_tweets.append(tweet)
            ManUtd_tweets_ids.append(tweet['id'])

            df = pd.DataFrame(ManUtd_tweets)

      

    except BaseException as error:
                   time.sleep(60)
                   print('Error', str(error))
    break



df

The above code times out after a while, i get the connection error 10054 - an existing connection was forceably closed by the remote host.

any ideas of how to get around this for tweepy?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source