-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Code.txt
More file actions
41 lines (32 loc) · 1.2 KB
/
Python_Code.txt
File metadata and controls
41 lines (32 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import tweepy
import pandas as pd
# Replace with your Twitter API credentials
BEARER_TOKEN = "Give your bearer_token"
# Initialize the Tweepy client
client = tweepy.Client(bearer_token=BEARER_TOKEN)
# Define the search query
query = "UkraineRussiaWar"
# Fetch recent tweets containing the query
try:
# Fetch tweets
response = client.search_recent_tweets(query=query, max_results=50, tweet_fields=["id", "text", "created_at"])
# Check if there are tweets in the response
if response.data:
# Store tweets in a container
attributes_container = []
for tweet in response.data:
attributes_container.append([tweet.id, tweet.text, tweet.created_at])
# Define columns for the DataFrame
columns = ["Tweet ID", "Tweet Text", "Created At"]
# Create DataFrame
tweets_df = pd.DataFrame(attributes_container, columns=columns)
# Print the DataFrame
print(tweets_df)
else:
print("No tweets found.")
except tweepy.TweepyException as e:
print('Error:', str(e))
to download file as csv
from google.colab import files
tweets_df.to_csv('tweets.csv')
files.download('tweets.csv')