How to include embedded links in tweet with tweepy?
Question:
I’m trying to create a program that will download tweets from a user timeline as per Client.get_users_tweets
method and look for external links inside those tweets.
From what I could gather, these objects reside into the entities
field, but so far my query
author_id = 12345
Client.get_users_tweets(author_id, tweet_fields = ['entities'])
yelds the tweets with an empty entity attribute.
I’m probably missing something in the attachments field, but I can’t figure out what.
Answers:
You gonna need the tweepy.Client
class in Tweepy v4.x for that, first install tweepy
pip install tweepy==4.3.0
Then you can use python like this for example:
import tweepy
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
BEARER_TOKEN = "your_bearer_token"
client = tweepy.Client(bearer_token=BEARER_TOKEN)
author_id = 12345
tweets = client.get_users_tweets(author_id, expansions=['entities.urls'])
for tweet in tweets:
if 'entities' in tweet and 'urls' in tweet.entities:
for url in tweet.entities.urls:
print(url.expanded_url)
Dont forget to replace API_KEY
, API_SECRET
, and BEARER_TOKEN
with your own credential.
I’m trying to create a program that will download tweets from a user timeline as per Client.get_users_tweets
method and look for external links inside those tweets.
From what I could gather, these objects reside into the entities
field, but so far my query
author_id = 12345
Client.get_users_tweets(author_id, tweet_fields = ['entities'])
yelds the tweets with an empty entity attribute.
I’m probably missing something in the attachments field, but I can’t figure out what.
You gonna need the tweepy.Client
class in Tweepy v4.x for that, first install tweepy
pip install tweepy==4.3.0
Then you can use python like this for example:
import tweepy
API_KEY = "your_api_key"
API_SECRET = "your_api_secret"
BEARER_TOKEN = "your_bearer_token"
client = tweepy.Client(bearer_token=BEARER_TOKEN)
author_id = 12345
tweets = client.get_users_tweets(author_id, expansions=['entities.urls'])
for tweet in tweets:
if 'entities' in tweet and 'urls' in tweet.entities:
for url in tweet.entities.urls:
print(url.expanded_url)
Dont forget to replace API_KEY
, API_SECRET
, and BEARER_TOKEN
with your own credential.