Định vị các xu hướng: Twitter Trends API (tiếp tục)
Các chủ đề xu hướng toàn cầu
Hãy lấy các chủ đề xu hướng toàn cầu ngày nay (các kết quả của bạn sẽ khác):
In [46]: world_trends = api.trends_place(id=1)
Phương thức trends_place trả về một list một phần tử chứa một dictionary. Key ‘trends’ của dictionary tham chiếu
đến một list các dictionaries trình bày mỗi trend:
In [47]: trends_list = world_trends[0][‘trends’]
Mỗi trend dictionary có name, url, promoted_content (chỉ ra tweet là một quảng cáo), query và tweet_volume keys
(thể hiện bên dưới). Xu hướng sau là ở Spanish – #BienvenidoSeptiembre có nghĩa “Welcome September”:
In [48]: trends_list[0]
Out[48]:
{‘name’: ‘#BienvenidoSeptiembre’,
‘url’: ‘http://twitter.com/search?q=%23BienvenidoSeptiembre’,
‘promoted_content’: None,
‘query’: ‘%23BienvenidoSeptiembre’,
‘tweet_volume’: 15186}
Cho các trends với hơn 10,000 tweets, tweet_volume là số các tweets, mặt khác, nó là None. Hãy sử dụng một hiểu
biết list để lọc ra list để rằng nó chứa chỉ các trends với hơn 10,000 tweets:
In [49]: trends_list = [t for t in trends_list if t[‘tweet_volume’]]
Tiếp theo, hãy xắp sếp các trends theo trật tự giảm dần theo tweet_volume:
In [50]: from operator import itemgetter
In [51]: trends_list.sort(key=itemgetter(‘tweet_volume’), reverse=True)
In [52]: for trend in trends_list[:5]:
…: print(trend[‘name’])
…:
#HBDJanaSenaniPawanKalyan
#BackToHogwarts
Khalil Mack
#ItalianGP
Alisson
