好的,所以我对python和编程很新。我认为我最好的选择是尝试使用对我来说有趣的东西。我在GitHub上找到了这个代码,并对它进行了一些编辑,以便它可以开始工作,但现在我遇到了一个错误,我无法弄清楚如何解决。
以下是代码,如果有人可以伸出援助之手?
import os
import sys
import requests
import numpy as np
import tweepy
from keras.models import Sequential
from keras.layers import Dense
from textblob import TextBlob
# First we login into twitter
consumer_key='xx'
consumer_secret='xx'
access_token='xx'
access_token_secret='xx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
user = tweepy.API(auth)
# Where the csv file will live
FILE_NAME = 'historical.csv'
def stock_sentiment(quote, num_tweets):
# Checks if the sentiment for our quote is
# positive or negative, returns True if
# majority of valid tweets have positive sentiment
list_of_tweets = user.search(quote, count=num_tweets)
positive, null = 0, 0
for tweet in list_of_tweets:
blob = TextBlob(tweet.text).sentiment
if blob.subjectivity == 0:
null += 1
next
if blob.polarity > 0:
positive += 1
if positive > ((num_tweets - null)/2):
return True
def get_historical(quote):
# Download our file from google finance
url = 'http://www.google.com/finance/historical?q=NASDAQ%3A'+quote+'&output=csv'
r = requests.get(url, stream=True)
if r.status_code != 400:
with open(FILE_NAME, 'wb') as f:
for chunk in r:
f.write(chunk)
return True
def stock_prediction():
# Collect data points from csv
dataset = []
with open(FILE_NAME) as f:
for n, line in enumerate(f):
if n != 0:
dataset.append(float(line.split(',')[1]))
dataset = np.array(dataset)
# Create dataset matrix (X=t and Y=t+1)
def create_dataset(dataset):
dataX = [dataset[n+1] for n in range(len(dataset)-2)]
return np.array(dataX), dataset[2:]
trainX, trainY = create_dataset(dataset)
# Create and fit Multilinear Perceptron model
model = Sequential()
model.add(Dense(8, input_dim=1, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=200, batch_size=2, verbose=2)
# Our prediction for tomorrow
prediction = model.predict(np.array([dataset[0]]))
result = 'The price will move from %s to %s' % (dataset[0], prediction[0][0])
return result
# Ask user for a stock quote
stock_quote = input('AAPL').upper()
# Check if the stock sentiment is positve
if not stock_sentiment(stock_quote, num_tweets=100):
print('This stock has bad sentiment, please re-run the script')
sys.exit()
# Check if we got te historical data
if not get_historical(stock_quote):
print('Google returned a 404, please re-run the script and')
print ('enter a valid stock quote from NASDAQ')
sys.exit()
# We have our file so we create the neural net and get the prediction
print (stock_prediction())
# We are done so we delete the csv file
os.remove(FILE_NAME)
但是当我运行它时,我收到了这些错误
Traceback (most recent call last):
File "/Users/MitchellWhite/Desktop/PythonProject/predict_stock_py-master/predict_stock.py", line 94, in <module>
if not stock_sentiment(stock_quote, num_tweets=100):
File "/Users/MitchellWhite/Desktop/PythonProject/predict_stock_py-master/predict_stock.py", line 29, in stock_sentiment
list_of_tweets = user.search(quote, count=num_tweets)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tweepy/binder.py", line 229, in execute
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 25, 'message': 'Query parameters are missing.'}]
关于最新进展的任何想法?我试着在twitter api上找到代码25,但那里没有运气。