I have these lines of code.just basically gets last 10-20 tweet from a person
import requests
import json
bearer_token='******'
userid=******
#url='https://api.twitter.com/2/users/by/username/{}'.format(username)
while True:
count = count 1
url='https://api.twitter.com/2/users/{}/tweets'.format(userid)
headers={'Authorization': 'Bearer {}'.format(bearer_token)}
response= requests.request('GET',url,headers=headers)
tweetsData=response.json()
print(json.dumps(tweetsData,indent=4,sort_keys=True))
I want to have last one only how? plz help me...
CodePudding user response:
According to the API documentation for this endpoint, the max_results
option can be applied:
max_results
Specifies the number of Tweets to try and retrieve, up to a maximum of 100 per distinct request. By default, 10 results are returned if this parameter is not supplied. The minimum permitted value is 5. It is possible to receive less than the max_results per request throughout the pagination process.`
So, modifying your url
, you can retrieve 5 Tweets as a minimum.
url='https://api.twitter.com/2/users/{}/tweets?max_results=5'.format(userid)
If you only want to process or display a single one, you will need to do something with the value of tweetsData
in order to truncate it to only a single Tweet.