J'ai écrit une fonction (ou j'essaie de) obtenir des statistiques d'un service de médias sociaux appelé Crowd Tangle et imprimer les statistiques pour les cinq premiers messages. J'essaie de comprendre comment utiliser une boucle pour passer la valeur de 0 à 4 dans la fonction pour appeler les bons nœuds JSON. J'utilise Python 3.6 et Spyder. Au lieu de copier la fonction cinq fois et d'écrire 0,1,2,3,4, est-il possible d'utiliser une boucle pour faire cela? Toute suggestion ou lien serait génial. Merci.Essayer de passer plusieurs valeurs à travers une fonction Python avec une boucle
import requests
def get_crowdtangle_stuff():
url = 'https://api.crowdtangle.com/posts?token=mytoken'
json_data = requests.get(url).json()
#print(json_data)
Platform = json_data['result']['posts'][0]['platform']
Platform_string = str(Platform)
print('This stupid thing was on the ' + Platform_string + '.')
Title = json_data['result']['posts'][0]['message']
Title_string = str(Title)
print('This stupid thing was on the ' + Title_string + '.')
Date = json_data['result']['posts'][0]['date']
Date_string = str(Date)
print('This stupid thing was posted on ' + Date_string)
Like_count = json_data['result']['posts'][0]['statistics']['actual']
['likeCount']
Like_count_string = str(Like_count)
print('This stupid thing got ' + Like_count_string + ' likes.')
Shares = json_data['result']['posts'][0]['statistics']['actual']
['shareCount']
Shares_string = str(Shares)
print('This stupid thing got ' + Shares_string + ' shares.')
Comments = json_data['result']['posts'][0]['statistics']['actual']
['commentCount']
Comments_string = str(Comments)
print('This stupid thing got ' + Comments_string + ' comments.')
Wow_count = json_data['result']['posts'][0]['statistics']['actual']
['wowCount']
Wow_count_string = str(Wow_count)
print('This stupid thing got ' + Wow_count_string + ' wows.')
Total_engagement = Like_count + Shares + Comments + Wow_count
Total_engagement_string = str(Total_engagement)
print('This stupid things total engagement score is ' +
Total_engagement_string + '.')
Link = json_data['result']['posts'][0]['link']
Link_string = str(Link)
print('This stupid thing has a link of ' + Link_string + '.')
get_crowdtangle_stuff()
Cela a parfaitement fonctionné. Merci! – Eric