I wrote following code and it works when I search for example “happy”. But once I want to pull replies to the posts of HSBC_UK that contain the words “credit card” it just doesn’t work anymore.
I am trying to program a sentiment analysis regarding HSBC_UKs credt card. If I use the advanced search from Twitter (X) itself I can find many many replies….
This is part of my code:
# Define search term and user ID
user_id = '2922732233'
search_term = 'credit card'
# Initialize variables
tweet_texts = []
max_tweets = 50 # Set the maximum number of tweets
# Function to clean tweets
def clean_tweet(tweet):
tweet = re.sub(r"httpS+|wwwS+|httpsS+", '', tweet, flags=re.MULTILINE)
tweet = re.sub(r'@w+|#', '', tweet)
tweet = re.sub(r'RT[s]+', '', tweet)
tweet = tweet.replace('n', ' ')
return tweet
# Function to fetch replies to a specific tweet
def fetch_replies(tweet_id, search_term, max_results=100):
query = f'conversation_id:{tweet_id} {search_term} is:reply lang:en'
replies = []
next_token = None
while True:
if next_token:
response = client.search_recent_tweets(query=query, tweet_fields=['id', 'text'], max_results=max_tweets, next_token=next_token)
else:
response = client.search_recent_tweets(query=query, tweet_fields=['id', 'text'], max_results=max_tweets)
if response.data:
tweets = response.data
else:
break
next_token = response.meta.get('next_token', None)
for tweet in tweets:
clean_text = clean_tweet(tweet.text)
replies.append(clean_text)
if len(replies) >= max_results:
break
if not next_token or len(replies) >= max_results:
break
return replies
# Fetch recent tweets from HSBC_UK
tweets_response = client.get_users_tweets(id=user_id, max_results=max_tweets, tweet_fields=['id', 'text'])
# Collect replies to these tweets
for tweet in tweets_response.data:
replies = fetch_replies(tweet.id, search_term, max_results=max_tweets)
tweet_texts.extend(replies)
if len(tweet_texts) >= max_tweets:
break
I tried already to put everythin in the search term but it resulted in nothingness
Felix Heidkamp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.