Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a tweet id from a tweet link using Tweepy

I am creating a bot for Reddit that posts a tweet's text, author, and a couple of other small things on submissions that directly link to the tweet (example - https://twitter.com/John_Yuki_Bot/status/889453450664304641).

However, I can't find anything in the Tweepy documentation that lets me extract a tweet's id from a tweet link so that I can use api.get_status(status_id) in order to get the status text, status author, and so on.

How can I get the status id just using links like that?

EDIT - The code at the end of the link (889453450664304641) is the status ID. I need to have this in its own variable so that Tweepy can use it.

like image 443
John Yuki Avatar asked Jan 27 '26 01:01

John Yuki


2 Answers

Simple

url.split('/')[-1]

and you get what you want

like image 119
Filippos Ser Avatar answered Jan 29 '26 15:01

Filippos Ser


url.split('/')[-1].split('?')[0]

the first part (.split('/')[-1]) splits the string based on the / character and chooses the last element. On top of that, we do .split('?')[0], which splits the result based on ? character and chooses the first part. This will remove any arguments being passed in the URL.

like image 25
Daksh Shah Avatar answered Jan 29 '26 16:01

Daksh Shah