'Problem with getting user created at and geo full name from the Twitter API using Python
For my thesis, I am currently trying to gather data from Twitter via their API. I am fairly new to coding, but I managed to get out some variables, but two do not seem to work. These are user created at (https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/user) and geo full name (https://developer.twitter.com/en/docs/twitter-api/expansions).
The code attached is working. I already tried things like tweet.geo['full_name'] or tweet.user.created_at but these do not work. Can someone explain how I can add the missing two variables?
import tweepy
import config
import pandas as pd
client = tweepy.Client(bearer_token=config.ACADEMIC_BEARER_TOKEN)
query = 'query -is:retweet'
start_time = '2021-01-30T00:00:00Z'
end_time = '2022-01-30T00:00:00Z'
response = client.search_all_tweets(query=query, max_results=100, start_time=start_time, end_time=end_time,
tweet_fields=['text', 'id', 'created_at', 'referenced_tweets', 'public_metrics'],
place_fields=['full_name'], user_fields=['created_at'],
expansions=['geo.place_id', 'author_id', 'in_reply_to_user_id'])
df = pd.DataFrame()
for tweet in response.data:
df = df.append({'text' : tweet.text, 'author_id' : tweet.author_id, 'tweet_id' : tweet.id,
'tweet.created_at' : tweet.created_at, 'in_reply_to_user_id' : tweet.in_reply_to_user_id,
'public_metrics' : tweet.public_metrics, 'referenced_tweets' : tweet.referenced_tweets,
'place_name' : tweet.geo}, ignore_index=True)
print(df)
Solution 1:[1]
There are only the id of the expanded objects (user and place in your case) in the response.data. The content of these objects is located in the response.includes.
The response of the Twitter API looks like that:
{
"data": [
{
"id": "...",
"author_id": "2244994945",
"geo": {
"place_id": "01a9a39529b27f36"
},
}
],
"includes": {
"places": [
{
"id": "01a9a39529b27f36",
"full_name": "Manhattan, NY"
}
],
"users": [
{
"id": "2244994945",
"created_at": "..."
}
],
}
}
So you have to get the author (/place) id in each tweet and, then, use this id to find the corresponding user (/place) in the included ones.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mickael Martinez |
