我正在上课,目的是观察特斯拉的股价与伊隆·马斯克自2016年以来的推文频率之间的相关性。
我使用pandas_datareader获取了股票数据,并使用推波获取了埃隆·马斯克(Elon Musk)的一些最新推文。
这是我的代码:
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime
import matplotlib.pyplot as plt
startDate = datetime(2016,1,1)
endDate = datetime.today()
tesla = web.DataReader('TSLA', 'yahoo', startDate, endDate)
tesla = tesla[["Adj Close"]]
tesla.to_excel('Tesla.xlsx')
plt.plot(tesla)
plt.show()
import tweepy
consumer_key = "MY KEY"
consumer_secret = "MY KEY"
access_key = "MY KEY"
access_secret = "MY KEY"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
name = "elonmusk"
tweetCount = 5
results = api.user_timeline(id=name, count=tweetCount)
for tweet in results:
print(tweet.text)
我希望将tweets放入具有两列的csv或xlsx文件中,一列包含日期信息,而另一列包含tweets。
代码返回以下内容:
@jlist ♥️
@moyn1ckzanyat правда
@v_g_fischer @4thFromOurStar Coming soon
@4thFromOurStar Already have one
Respawn++ https://twitter.com/cleantechnica/status/1119828529422770177
Process finished with exit code 0