I’m trying to compile the data result into an excel file, but when i run it the only output that i got is id, name, and username. i didn’t get any data from it at all. Here is the code that i use
ssl._create_default_https_context = ssl._create_unverified_context id, name, and username
# Oauth and client creation
bearer_token = "token"
client = tweepy.Client(bearer_token)
x = input()
q = (f"conversation_id:{x}")
tweets = []
users = []
for tweet_batch in tweepy.Paginator(client.search_recent_tweets, query=q,
tweet_fields=['public_metrics', 'author_id'],
user_fields=['name','username'],
max_results=100, expansions='author_id'):
for tweet in tweet_batch.includes["users"]:
tweet_df = pd.DataFrame(tweet, columns=["ID"])
But if i do this
tweet_df = pd.DataFrame(tweet, columns=["ID", "Name", "Username"])
It doesn’t output the result too, but instead it says that passed value is (3,1), indices imply (3, 2). Whar should i do so that it would output result into excel file or the DataFrames?
Firstly, beware of excel and twitter data: Excel corrupts IDs and makes them unreadable if parsed as an integer - all IDs should be TEXT column type, otherwise the data is corrupted and lost.
In tweepy, you can get the other fields by specifying the tweet_fields and expansions parameters: Expansions and Fields — tweepy 4.12.1 documentation
If all you need are recent tweet results and a CSV, I can recommend using twarc in the command line:
And you’ll get a good CSV to load as a dataframe using twarc-csv package GitHub - DocNow/twarc-csv: A plugin for twarc2 for converting tweet JSON into DataFrames and exporting to CSV. . Or you can use twarc as a library and get a Dataframe in code Examples of using twarc2 as a library - twarc
It’s designed for Academic access, but works just as well for Essential or Elevated, you just won’t be able to use --archive to get older than 7 days of tweets.