I tried using tweepy and TwitterAPI but both those solutions do not work because I have Essential access to Twitter developer account and not Elevated (elevated app got rejected).
Basically doing:
- tweepy:
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the ID of the status
id = 1272771459249844224
# fetching the status
status = api.get_status(id)
# fetching the text attribute
text = status.text
- Twitter API:
r = api.request('statuses/show/:%d' % 210462857140252672)
print(r.text)
Both of those solutions do not work. I think all the solutions that include access keys stuff will not work.
I also tried some web scraping but it also does not work. This is what I tried:
from lxml import html
import requests
page = requests.get('https://twitter.com/UniteAlbertans/status/572342978255048705')
print(page.text)
tree = html.fromstring(page.content)
tree.xpath('//span[@class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"]/text()')
But I do not think that works because when I print the page text I do not even see the text of the tweet.
Could someone help me how I could get the text of the tweet given tweet ID through code?
Thank you so much 🙂
2
Answers
This works using Tweepy and the v2 Twitter API (documentation here):
If you have Essential Access, then you can just use the Tweepy Twitter API v2 methods. So generally, you can’t access
tweepy.API
, instead you have to find a way withtweepy.Client
.To get the text of a tweet by tweet id, try:
Be sure, that you have installed the latest version of tweepy. In my case i had tweepy version 4.4.0.