数据挖掘 , 编程 (Data Mining, Programming)
Getting Twitter data
获取Twitter数据
Let’s use the Tweepy package in python instead of handling the Twitter API directly. The two things we will do with the package are, authorize ourselves to use the API and then use the cursor to access the twitter search APIs.
让我们在python中使用Tweepy包,而不是直接处理Twitter API。 我们将对该软件包执行的两件事是,授权自己使用API,然后使用光标访问twitter搜索API。
Let’s go ahead and get our imports loaded.
让我们继续加载我们的导入。
import tweepy
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as npsns.set()
%matplotlib inline
Twitter授权 (Twitter authorization)
To use the Twitter API, you must first register to get an API key. To get Tweepy just install it via pip install Tweepy. The Tweepy documentation is best at explaining how to authenticate, but I’ll go over the basic steps.
要使用Twitter API,您必须首先注册以获得API密钥。 要获取Tweepy,只需通过pip安装Tweepy即可安装。 Tweepy文档最擅长于说明如何进行身份验证,但我将介绍一些基本步骤。
Once you register your app you will receive API keys, next use Tweepy to get an OAuthHandler. I have the keys stored in a separate config dict.
一旦注册您的应用程序,您将收到API密钥,接下来请使用Tweepy获取OAuthHandler。 我将密钥存储在单独的配置字典中。
config = {"twitterConsumerKey":"XXXX", "twitterConsumerSecretKey" :"XXXX"}
auth = tweepy.OAuthHandler(config["twitterConsumerKey"], config["twitterConsumerSecretKey"])
redirect_url = auth.get_authorization_url()
redirect_url