skip to Main Content

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


  1. This works using Tweepy and the v2 Twitter API (documentation here):

    Client.get_tweet(id)
    
    Login or Signup to reply.
  2. 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 with tweepy.Client.

    To get the text of a tweet by tweet id, try:

    import tweepy
    
    # token for twitter developers
    client = tweepy.Client(
        bearer_token="<bearer_token>",
        consumer_key="<consumer_key>",
        consumer_secret="<consumer_secret>",
        access_token="<access_token>",
        access_token_secret="<access_token_secret>",
    )
    
    # find tweet by id
    tweet = client.get_tweet(id=<tweet_id_here>)
    
    # extract data from tweet
    text = tweet.data
    print(text)
    

    Be sure, that you have installed the latest version of tweepy. In my case i had tweepy version 4.4.0.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search