Recuperer user id d'une réponse
Résolu
Bonjour,
J'essaye d'utiliser Tweepy pour expérimenter un bot sur Twitter.
Je voudrais obtenir l'id d'un utilisateur. Dans la doc il y a cela:
Client.get_user(*, id, username, user_auth=False, expansions, tweet_fields, user_fields)
#Connexion
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)
#Obtention de l'id
screen_name='PerrySaylorWEB3'
user = client.get_user(username=screen_name)
print(user)
J'obtiens cela:
Response(data=<User id=1448190092275683343 name=Perry Saylor username=PerrySaylorWEB3>, includes={}, errors=[], meta={})
Comment puis je récupérer juste le User id?
Merci d'avance
J'essaye d'utiliser Tweepy pour expérimenter un bot sur Twitter.
Je voudrais obtenir l'id d'un utilisateur. Dans la doc il y a cela:
Client.get_user(*, id, username, user_auth=False, expansions, tweet_fields, user_fields)
#Connexion
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)
#Obtention de l'id
screen_name='PerrySaylorWEB3'
user = client.get_user(username=screen_name)
print(user)
J'obtiens cela:
Response(data=<User id=1448190092275683343 name=Perry Saylor username=PerrySaylorWEB3>, includes={}, errors=[], meta={})
Comment puis je récupérer juste le User id?
Merci d'avance
6 réponses
-
ModérateurBonjour,
Déjà.. quand tu postes du code sur le forum, tu dois utiliser les BALISES DE CODE.
Explications ( à lire entièrement !! ) disponibles ici :
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Ensuite, dans la doc il semble y avoir un exemple
https://www.geeksforgeeks.org/python-tweepy-getting-the-id-of-a-user/# import the module import tweepy # assign the values accordingly consumer_key = "" consumer_secret = "" access_token = "" access_token_secret = "" # 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 screen name of the user screen_name = "geeksforgeeks" # fetching the user user = api.get_user(screen_name) # fetching the ID ID = user.id_str print("The ID of the user is : " + ID)
-
Merci pour ta réponse mais leur exemple ne fonctionne plus depuis la dernière MAJ.
Si je lance leur exemple avecuser = api.get_user(screen_name)
J'ai cet erreurTypeError: get_user() takes 1 positional argument but 2 were given
Si je fais comme j'ai fait dans mon 1er message en ajoutant username=
user = api.get_user(username=screen_name)
, cette fois j'obtiens cela:
Unexpected parameter: username
et l'on me demande d'elever mon acces
tweepy.errors.Forbidden: 403 Forbidden 453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
Je n'arrive pas à utiliser l'API. Par contre avec le Client cela fonctionne mieux.
J'ai tenté de faire cela du coup:
#Follow screen_name='PerrySaylorWEB3' # fetching the user user = client.get_user(username=screen_name) print(user) # fetching the ID ID = user.id_str print("The ID of the user is : " + ID)
J'obtiens bien un retour cette fois avec le print(user) mais le reste ne fonctionne pas
Response(data=<User id=1448190092275683343 name=Perry Saylor username=PerrySaylorWEB3>, includes={}, errors=[], meta={}) Traceback (most recent call last): File "/Users/Test.py", line 28, in <module> ID = user.id_str AttributeError: 'Response' object has no attribute 'id_str'
D'ou ma question dans le premier message? Comment récupérer le user id?
Merci -
Modérateur
-
-
Oui... J'obtiens cette erreur
AttributeError: 'Response' object has no attribute 'id'
-
-
Contributeur@jordane45Response est le type de la variable user.
Voir https://docs.tweepy.org/en/stable/client.html#response
moi je ferais plutôt:
print(type(user))
print (user.data)
print (type(user.data)) -
@yg_betype(user) donne <class 'tweepy.client.Response'>
user.data donne PerrySaylorWEB3
type(user.data) donne <class 'tweepy.user.User'> -
Contributeur@LucDantonalors peut-être ainsi:
resp= api.get_user(screen_name) user=resp.data ID = user.id_str print("The ID of the user is : " + ID)
-
-
le print user.Response donne:
AttributeError: 'Response' object has no attribute 'Response'
-
-
@jordane45Je n'arrive pas à trouver la version... quand je fais pip list il n'y a pas de tweety
J'ai fait python3 -m pip install tweepy
Pour le code c'est ici:
https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9
-
-
Contributeurbonjour,
pour contourner le problème, j'essaierais ceci:rr=repr(user) print("rr:",rr) -
Ca me donne cela:
rr: Response(data=<User id=1448190092275683343 name=Perry Saylor username=PerrySaylorWEB3>, includes={}, errors=[], meta={}) -
Afin d'obtenir l'user id il faut mettre cela:
print(user.data.id)