在Application Management的Keys and Access Tokens,点击Generate Access Token.
利用tweepy获取特朗普最近推文
# -*- coding: utf-8 -*-"""
Created on Fri Jan 6 18:31:59 2017@author: caofk
"""import re
import tweepy
import time
import tweepy
#https://github.com/tweepy/tweepyimport random
import pandas as pd#Twitter API credentials
consumer_key = " "
consumer_secret = " "
access_key = " "
access_secret = " "screen_name = "realDonaldTrump"#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)#initialize a list to hold all the tweepy Tweets
alltweets = [] #make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=50)#save most recent tweets
alltweets.extend(new_tweets)#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1#keep grabbing tweets until there are no tweets left to grabwhile len(new_tweets) > 0:print("getting tweets before %s" % (oldest))#all subsiquent requests use the max_id param to prevent duplicatesis_finished = 0while is_finished == 0:try:new_tweets = api.user_timeline(screen_name = screen_name,count=50,max_id=oldest)is_finished = 1except Exception as e:print(e)time.sleep(random.choice(range(300, 600)))is_finished = 0#save most recent tweetsalltweets.extend(new_tweets)#update the id of the oldest tweet less oneoldest = alltweets[-1].id - 1print("...%s tweets downloaded so far" % (len(alltweets)))outtweets = pd.DataFrame()
outtweets["推文ID"] = [tweet.id_str for tweet in alltweets]
outtweets["推文创建时间"] = [tweet.created_at for tweet in alltweets]
outtweets["推文内容"] = [re.sub(r'\s+'," ", tweet.text) for tweet in alltweets]
outtweets.to_excel("E:\\"+ screen_name + 's_tweets.xls')
twitter注册不了 Social networks are always trying to get you to “engage” more with their service and recently, Twitter has introduced a new trick: Highlights notifications. 社交网络一直在努力让您与他们的服务“互动”,最近,Twitter引入…