Social Analytics companies have been massively using twitter to get insights about whatever data they are interested in for brands, celebrities, etc. and also trending topics. In this tutorial, you’ll know how to get countries that have trends on Twitter and also get insights about what topics are trending the most and be able to retrieve the URL of that tweet and its volume as well.
社交分析公司一直在大量使用Twitter,以获取他们对品牌,名人等感兴趣的任何数据的洞察力,以及热门话题。 在本教程中,您将知道如何获取在Twitter上具有趋势的国家,以及有关哪些主题最流行的见解,以及如何检索该推文的URL及其数量。
Here I’ll walk you through how you can do that with Python and Tweepy. You can do many other things with Tweepy other than trending topics but in this tutorial, I will focus on getting trends. Let’s dive in and see what Tweepy can do for us.
在这里,我将引导您逐步了解如何使用Python和Tweepy做到这一点。 除了趋势主题外,您还可以使用Tweepy做其他许多事情,但是在本教程中,我将重点介绍趋势。 让我们潜入水中,看看Tweepy能为我们做什么。
Tweepy is an API wrapper for twitter; you can use it to get some data from twitter, to know some information about your account, and get some insights about public data.
Tweepy是Twitter的API包装器; 您可以使用它来获取来自Twitter的一些数据,了解有关您帐户的一些信息,并获得有关公共数据的一些见解。
Tweepy is in PyPy, so you can use pip to install it:
Tweepy在PyPy中,因此您可以使用pip进行安装:
pip install tweepyAn alternative way to do it is to install it from its GitHub repo:
另一种方法是从GitHub存储库中安装它:
pip install git+https://github.com/tweepy/tweepy.gitTweepy supports Python 2.7, 3.5, 3.6, 3.7, & 3.8
Tweepy支持Python 2.7、3.5、3.6、3.7和3.8
The first thing you need to do is to have a twitter developer account. If this is your first to set up a twitter developer account, it may take time from twitter’s side to accept your request. Once your account is accepted, you can click on the Developer Portal tab at your developer account and hover over your name and select Apps from the dropdown. There you can create a new app, you’ll be asked some questions regarding your app. You should generate your tokens and it’s a good practice to save them at a password manager like passpack for example; it’s a free software for storing and sharing passwords.
您需要做的第一件事是拥有一个Twitter开发人员帐户 。 如果这是您首次建立Twitter开发者帐户,则Twitter可能需要一些时间才能接受您的请求。 接受您的帐户后,您可以单击开发人员帐户上的“开发人员门户”选项卡,将鼠标悬停在您的姓名上,然后从下拉列表中选择“应用程序”。 在这里您可以创建一个新的应用程序,系统会询问您有关应用程序的一些问题。 您应该生成令牌,将其保存在密码管理器(例如passpack)中是一种很好的做法。 这是一个用于存储和共享密码的免费软件。
Now, you have four credentials for your app:
现在,您为应用程序提供了四个凭据:
API Key API密钥 API secret key API密钥 Access token 访问令牌 Access token secret 访问令牌机密Let’s understand why API keys are different from access tokens. The short answer is API keys are used for authorization, while tokens are for authentication. What does that mean?
让我们了解为什么API密钥与访问令牌不同。 简短的答案是API密钥用于授权,而令牌用于身份验证。 那是什么意思?
Before we know that, we need to know what we should do first to do our task. What you want is to let twitter know who you are to give you access to the data that you want. Twitter gives you permissions through authorization and then asks you to generate tokens which know who you claim to be (authentication), this is like logging in twitter account after you’ve been authorized that you have the privilege to read and/or write access.
在我们知道这一点之前,我们需要知道我们应该首先做什么以完成我们的任务。 您想要的是让Twitter知道您是谁,从而使您可以访问所需的数据。 Twitter通过授权授予您权限,然后要求您生成令牌,这些令牌知道您声称自己是谁(身份验证),这就像在您被授权拥有读取和/或写入访问权限后登录Twitter帐户一样。
It’s recommended not to hardcode these credentials in our script so better to have them as environment variables. Here is the script so far:
建议不要在我们的脚本中对这些凭据进行硬编码,这样最好将它们作为环境变量使用。 到目前为止,这是脚本:
What we need to do is to let our script know our environment variables. So let’s assign them through the command line:
我们需要做的是让我们的脚本知道我们的环境变量。 因此,让我们通过命令行分配它们:
$ export API_KEY='Your API Key'$ export API_SECRET_KEY='Your API Secret Key'$ export ACCESS_TOKEN='Your Access Token'$ export ACCESS_TOKEN_SECRET='Your Access Token Secret'If you are interested in getting available locations that have trends at twitter around the world, you can use Tweepy’s method trends_available()
如果您有兴趣获取世界各地Twitter上具有趋势的可用位置,则可以使用Tweepy的方法Trends_available()
Let’s see what we have until now:
让我们来看看到目前为止所拥有的:
Let’s say we want to get the trending tweets in Egypt. Instead of hardcoding ‘Egypt’ in the script, we can pass it as an argument variable.
假设我们想获得埃及的流行推文。 代替在脚本中对“埃及”进行硬编码,我们可以将其作为参数变量传递。
Tweepy has a method trends_closest() and trends_place() — combining the two gives us very similar result to what we’re looking for, trends_closest just needs a longitude and latitude of the country we want and then it can return a JSON file that has WOEIDs that we’re interested in. WOEID (or Where On Earth IDentifier) is a unique identifier for any feature on earth. For example, WOEID for New York is 2459115, and WOEID for Los Angeles is 2442047. Although both reside in the United States, they both have unique WOEID.
Tweepy有一个trends_closest()和trends_place()方法 —将两者结合在一起,我们得到的结果与我们想要的结果非常相似, trends_closest只需要我们想要的国家的经度和纬度,然后它可以返回一个包含WOEIDs是我们感兴趣的。 WOEID (或在地球上Identifier)是地球上任何功能的唯一标识符。 例如,纽约的WOEID为2459115,洛杉矶的WOEID为2442047。尽管两者都居住在美国,但它们都具有唯一的WOEID。
One note to consider, just install geocoder which is a library that helps you get location-relevant information and we need it here to get the longitude and latitude.
需要注意的一点是,只需安装geocoder ,这是一个可帮助您获取与位置相关的信息的库,我们在这里需要它来获取经度和纬度。
Make sure when you run the final script to add the argument variable of the country that you want:
确保在运行最终脚本时添加所需国家/地区的参数变量:
python tweetstrending.py EgyptEnjoy!
请享用!
import tweepy import os import json import sys import geocoder # API Keys and Tokens consumer_key = os.environ['API_KEY'] consumer_secret = os.environ['API_SECRET_KEY'] access_token = os.environ['ACCESS_TOKEN'] access_token_secret = os.environ['ACCESS_TOKEN_SECRET'] # Authorization and Authentication auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) if __name__ == "__main__": # Available Locations available_loc = api.trends_available() # writing a JSON file that has the available trends around the world with open("available_locs_for_trend.json","w") as wp: wp.write(json.dumps(available_loc, indent=1)) # Trends for Specific Country loc = sys.argv[1] # location as argument variable g = geocoder.osm(loc) # getting object that has location's latitude and longitude closest_loc = api.trends_closest(g.lat, g.lng) trends = api.trends_place(closest_loc[0]['woeid']) # writing a JSON file that has the latest trends for that location with open("twitter_{}_trend.json".format(loc),"w") as wp: wp.write(json.dumps(trends, indent=1))Now, you can use Tweepy to get some the latest 50 trending topics on Twitter, but make sure of the number of requests you have if you’re using it extensively .. because you have a limit of 100,000 requests per day. That’s according to Twitter on June 19, 2019
现在,您可以使用Tweepy在Twitter上获得一些最新的50个热门话题,但是如果您广泛使用它,请确保拥有的请求数..因为每天的请求数量限制为100,000。 这是根据Twitter上的2019年6月19日
Tweepy has a lot more than trending methods, I hope this tutorial is useful and maybe motivational to read more about Tweepy.
Tweepy不仅提供趋势方法,我希望本教程对阅读有关Tweepy的内容很有用,并且可能对您有所启发。
Thank you for reaching here!
感谢您到达这里!
http://docs.tweepy.org/en/latest/index.html
http://docs.tweepy.org/en/latest/index.html
https://medium.com/gopypi/100-scripts-in-30-days-challenge-script-18-19-20-getting-trending-topics-on-twitter-using-tweepy-d740880d5681
https://medium.com/gopypi/100-scripts-in-30-days-challenge-script-18-19-20-getting-trending-topics-on-twitter-using-tweepy-d740880d5681
https://dev.to/towernter/tweeting-with-python-5fj5
https://dev.to/towernter/tweeting-with-python-5fj5
翻译自: https://medium.com/analytics-vidhya/how-to-get-trending-tweets-in-any-country-with-python-and-tweepy-af2bfe760251
相关资源:tweets_collection:使用Tweepy从Twitter帐户提取推文。 GitHub和MongoDB-源码