python创建api接口

tech2023-06-18  108

python创建api接口

This tutorial illustrates how to use a Python API to connect to a Twitter account using the Twitter library. Specifically, this API allows a user to extract high quantities of data pertaining to a specific Twitter account, as well as directly control Twitter posts from the Python platform (such as posting multiple tweets at once).

本教程说明了如何使用Python API使用Twitter库连接到Twitter帐户。 具体来说,该API允许用户提取与特定Twitter帐户有关的大量数据,并直接控制来自Python平台的Twitter帖子(例如一次发布多个tweet)。

Even if you’re not a regular Python user, using one of Python’s Twitter libraries is quite useful when it comes to analytics. For instance, while web developers might be more inclined to use a language such as PHP in connecting to the API, Python offers more flexibility in analyzing trends and statistics with the data. Therefore, data scientists and other analysts would find Python to be a better fit for this purpose.

即使您不是普通的Python用户,在进行分析时,使用Python的Twitter库之一也非常有用。 例如,虽然Web开发人员可能更倾向于使用PHP之类的语言来连接到API,但是Python在分析趋势和数据统计方面提供了更大的灵活性。 因此,数据科学家和其他分析家会发现Python更适合此目的。

In this tutorial, we’ll start with some basic steps in connecting Python to the Twitter API, and then look at how to stream the desired data. Note that, while the Twitter library (and other Python libraries such as Tweepy and Twython) can carry out a multitude of different tasks with the data, I’ll focus on some of the more basic (and useful) queries in this article. Specifically, I’ll walk through how to:

在本教程中,我们将从将Python连接到Twitter API的一些基本步骤开始,然后研究如何流式传输所需的数据。 请注意,尽管Twitter库(以及其他Python库,如Tweepy和Twython )可以对数据执行许多不同的任务 ,但在本文中,我将重点介绍一些更基本(更有用)的查询。 具体来说,我将逐步介绍如何:

connect Python to the Twitter API using the appropriate credentials

使用适当的凭据将Python连接到Twitter API download tweets associated with the specific account

下载与特定帐户关联的推文 download a list of all following and followed users for an account

下载帐户的所有关注和关注用户的列表 post multiple tweets at once

一次发布多条推文 customize a search for instances of a specific term on Twitter.

在Twitter上针对特定术语的实例自定义搜索。

1.将Python连接到Twitter API (1. Connect Python to the Twitter API)

This tutorial uses iPython as the Python interface to connect to Twitter. In order to connect to the API, we need to obtain the Consumer Key, Consumer Secret, and Access Token Secret.

本教程使用iPython作为连接Twitter的Python界面。 为了连接到API,我们需要获取Consumer Key , Consumer Secret和Access Token Secret 。

To obtain these, you need to sign into your account at apps.twitter.com. Once there, you’ll be prompted to create an application:

要获取这些,您需要在apps.twitter.com上登录您的帐户。 到达该位置后,系统将提示您创建一个应用程序:

Once you have the application created, you’ll find the relevant keys and tokens under the Keys and Access Tokens section.

创建应用程序后,您将在“ 密钥和访问令牌”部分下找到相关的密钥和令牌。

Firstly, we install the python-twitter library in our terminal as follows:

首先,我们在终端中安装python-twitter库,如下所示:

pip install python twitter

Once we’ve done that, we import the Twitter library and enter the credentials as follows:

完成此操作后,我们将导入Twitter库并输入凭据,如下所示:

import twitter api = twitter.Api(consumer_key='your_consumer_key', consumer_secret='your_consumer_secret', access_token_key='your_access_token_key', access_token_secret='your_access_token_secret') print(api.VerifyCredentials())

As soon as the correct credentials are entered, the connection to the API is complete, and we’re now able to control our Twitter account through the Python platform!

输入正确的凭据后,与API的连接即告完成,我们现在可以通过Python平台控制Twitter帐户!

2.下载用户时间表 (2. Download User Timeline)

Now that we have connected Python to the Twitter API, we can go ahead and start utilizing different Twitter functions remotely. For instance, if we wish to download a user timeline of tweets, we do this using api.GetUserTimeline as below (and specifying the screen name for the appropriate account), and then using the print() function to display the results:

现在我们已经将Python连接到Twitter API,我们可以继续并开始远程使用不同的Twitter功能。 例如,如果要下载tweets的用户时间轴,请使用如下所示的api.GetUserTimeline (并为相应帐户指定屏幕名称),然后使用print()函数显示结果:

statuses = api.GetUserTimeline(screen_name='Michael Grogan') print([s.text for s in statuses])

Once we’ve inputted the above, we see the respective timeline displayed in the Python interface:

输入上述内容后,我们将在Python界面中看到相应的时间轴:

3.下载关注和关注的联系人 (3. Download Following and Followed Contacts)

The Twitter library also enables us to download a list of accounts that a particular user is following, as well as accounts that are followers of that particular user. To do this, we use api.GetFriends() for the former, and api.GetFollowers() for the latter:

Twitter库还使我们能够下载特定用户正在关注的帐户列表,以及该特定用户的追随者帐户。 要做到这一点,我们使用api.GetFriends()对于前者,并api.GetFollowers()后者:

users = api.GetFriends() print([u.name for u in users]) followers = api.GetFollowers() print([f.name for f in followers])

Note that we can also set an upper bound on the number of users we wish to fetch. For instance, if we wish to fetch 100 followers for any particular account, we can do so by adding a total_count variable to the function as follows:

请注意,我们还可以为希望获取的用户数设置上限。 例如,如果我们希望为任何特定帐户获取100个关注者,则可以通过如下向函数添加total_count变量来做到这一点:

followers = api.GetFollowers(total_count=100) print([f.name for f in followers])

4.发表多条推文 (4. Post Multiple Tweets)

One of the neat things about using a Twitter API is the ability to post multiple tweets at once. As an example, we can post the below two tweets concurrently using the api.PostUpdate command (again, using the print() function to confirm). Once we then go to the Twitter account in question, we see that both tweets have been posted:

使用Twitter API的整洁的事情之一是能够一次发布多个tweet。 例如,我们可以使用api.PostUpdate命令同时发布以下两条推文(同样,使用print()函数进行确认)。 然后,当我们转到有问题的Twitter帐户时,我们看到两条推文都已发布:

status = api.PostUpdate('How to calculate the Variance Inflation Factor in R: http://www.michaeljgrogan.com/ordinary-least-squares-an-analysis-of-stock-returns/ #rstats #datascience #programming') print(status.text) status = api.PostUpdate('#BigData Scientists Earn 10X to 15X More Money Compared to Engineers, CAs http://bit.ly/1NoAgto #datascience') print(status.text)

5.搜索推文 (5. Search Tweets)

The GetSearch() function included with the twitter library is a particularly powerful tool. This function allows us to search for a specific term across Twitter. Note that this applies for all users who have inputted a particular term — not simply the account we’ve provided credentials for in Python.

twitter库中包含的GetSearch()函数是一个特别强大的工具。 此功能使我们可以在Twitter上搜索特定术语。 请注意,这适用于输入了特定术语的所有用户-不仅仅是我们在Python中提供了凭据的帐户。

As an example, let’s conduct a search for the term “bigdata” in Python. The parameters we set are those tweets since 21st November 2016 that contain the term, and we’re choosing to cap the number of tweets streamed at 10:

例如,让我们在Python中搜索术语“ bigdata”。 我们设置的参数是自2016年11月21日以来包含该术语的推文,我们选择将流式推文的数量限制为10:

api.GetSearch(term='bigdata', since=2016-11-21, count=10)

Note that we can customize the GetSearch() function in a variety of ways, depending on how we wish to extract the data. For instance, though this will take significantly longer to stream if no date is specified, we could also choose to gather tweets before 21st November 2016 that contain the term “bigdata” as follows:

请注意,根据我们希望提取数据的方式,我们可以通过多种方式自定义GetSearch()函数。 例如,尽管如果没有指定日期,这将花费更长的时间流式传输,但是我们也可以选择在2016年11月21日之前收集包含术语“ bigdata”的推文,如下所示:

api.GetSearch(term='bigdata', until=2016-11-21, count=10)

It’s worth bearing in mind that this function downloads up to 7 days’ worth of data prior to the date we specify under the until variable.

这一点值得铭记这个功能下载至7天的价值之前的日期数据,我们的下指定until变量。

Additionally, we’re not limited to merely searching by terms with GetSearch. For instance, suppose that we wish to search tweets by geolocation — specifically tweets sent since 18th November within a 1 mile radius of Times Square, New York (note that distance can be formatted in either miles or kilometers using mi or km respectively):

此外,我们不仅限于使用GetSearch进行术语搜索。 例如,假设我们希望按地理位置搜索推文,特别是自11月18日以来在纽约时代广场半径1英里范围内发送的推文(请注意,距离可以分别用mi或km表示为英里或公里):

api.GetSearch(geocode="40.758896,-73.985130,1mi", since=2016-11-18)

Upon running the GetSearch() function, we see that Python returns the following tweets (and of course, what better place to find Donald Trump!):

运行GetSearch()函数后,我们看到Python返回以下推文(当然,还有找到唐纳德·特朗普更好的地方!):

如何使用这些数据? (How Can This Data Be Used?)

One of the particular reasons why Python is highly attractive for streaming social network data is — as mentioned before — the ability to conduct in-depth data analysis on the information we collect.

如前所述,Python之所以对流式社交网络数据具有高度吸引力的特殊原因之一是能够对我们收集的信息进行深入的数据分析。

For instance, we’ve already seen how to search tweets by location using GetSearch. With machine learning being all the rage among data scientists involved in analyzing social media trends, one particular technique that has become quite popular in this area is network analysis. This is a technique where dispersed data (or nodes) can in fact be shown to form close networks — typically with certain nodes proving to be a focal point. As an example, suppose we were to analyze the 1000 most popular tweets in ten different locations across the world.

例如,我们已经了解了如何使用GetSearch按位置搜索推文。 由于机器学习已成为参与分析社交媒体趋势的数据科学家中的头等大事,网络分析是在该领域非常流行的一种特殊技术。 这是一种实际上可以显示分散的数据(或节点)形成紧密网络的技术,通常某些节点被证明是焦点。 例如,假设我们要分析全球十个不同位置的1000条最受欢迎的推文。

On a random day, we might find that hashtags on tweets in London vary significantly from those of tweets in New York, even though we likely still see some coherency between the different tweets in the network. However, during a major world event such as US election night or Brexit, when Twitter is trending on that particular topic, it’s found that networks tend to be much more close-knit, and as a result, there’s more opportunity for sentiment analysis under such a scenario — for example, as it becomes evident who will win the presidency, or that Britain is voting to leave the EU. One will typically see networks cluster in different fashions, depending on the trending tweets, as further real-time information becomes available.

在随机的一天中,我们可能会发现,伦敦的推文上的标签与纽约的推文上的标签存在显着差异,即使我们仍可能看到网络中不同推文之间的一致性。 但是,在诸如美国大选之夜或英国脱欧之类的重大世界性事件中,当Twitter围绕该特定主题发展趋势时,人们发现网络之间的联系更加紧密,因此,在这种情况下,存在更多进行情感分析的机会一种情况-例如,很明显谁将赢得总统职位,或者英国正在投票决定退出欧盟。 随着更多的实时信息变得可用,人们通常会看到网络以不同的方式群集,具体取决于趋势推文。

This is just one of the advantages of Python. While it’s one thing to use an API to connect to Twitter (which can be done in many programming languages), it’s another to be able to employ analytics to sort through that data in a meaningful way. Machine learning techniques can be employed through Python to both analyze streamed data from social networks and make meaningful predictions from that data.

这只是Python的优点之一。 使用API​​连接到Twitter是一回事(可以用多种编程语言来完成),而能够利用分析以有意义的方式对数据进行分类则是另一回事。 可以通过Python使用机器学习技术来分析来自社交网络的流数据并根据该数据做出有意义的预测。

结论 (Conclusion)

The module documentation provides a highly detailed description of the different functions that can be used in Python to download, filter and manipulate data. Ultimately, while we’ve also looked at ways to directly post to Twitter using the API, the techniques described above are particularly useful when it comes to analysis of trends — such as hashtag popularity, frequency of search terms by location, and so on. In this regard, interacting with Twitter through Python is particularly useful for those who wish to implement data analysis techniques on the information gathered.

模块文档高度详细地描述了Python中可用于下载,过滤和处理数据的各种功能。 最终,尽管我们也研究了使用API​​直接发布到Twitter的方法,但是上述技术在分析趋势时特别有用,例如标签的流行度,按位置搜索字词的频率等。 在这方面,对于希望对收集到的信息实施数据分析技术的人,通过Python与Twitter交互特别有用。

Of course, API interaction with Twitter can be done in a wide variety of languages, depending on what your end goal is. If the goal is web development or design, then PHP or Ruby might be your best bet. However, if your goal is to use data obtained from Twitter to conduct meaningful analysis, then Python is in a league of its own. In this context, I highly recommend Python when it comes to doing some serious number-crunching with Twitter.

当然,取决于最终目标是什么,API与Twitter的交互可以多种语言完成。 如果目标是Web开发或设计,那么PHP或Ruby可能是最好的选择。 但是,如果您的目标是使用从Twitter获得的数据进行有意义的分析,则Python属于自己的联盟​​。 在这种情况下,在使用Twitter进行严格的数字运算时,我强烈建议使用Python。

翻译自: https://www.sitepoint.com/how-to-create-a-twitter-app-and-api-interface-via-python/

python创建api接口

相关资源:Twitter_Bot_with_Python3:如果您具有Twitter API。 您可以使用此应用程序-源码
最新回复(0)