当单击index.html上的提交按钮时,我还需要将在extract_tweets函数中以csv格式收集和编写的推文也实时(流)打印在html页面“ livestream.html”上。 我已经在index.html src到livestream.html上创建了iframe。 当前livestream.html是空的html文档。
main.py
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
@app.route('/pie', methods=['GET', 'POST'])
def pie():
if request.method == 'POST':
keyword = request.form['keyword']
extract_tweets(keyword)
return 'Search Complete'
if __name__ == '__main__':
app.run(debug=True)
提取推文
def extract_tweets( query):
auth = OAuthHandler(config.consumer_key, config.consumer_secret)
auth.set_access_token(config.access_token, config.access_secret)
api = tweepy.API(auth)
csvFile = open('result.csv', 'w', newline='', encoding="utf-8")
csvWriter = csv.writer(csvFile)
for index, search in enumerate(tweepy.Cursor(api.search, q=query, lang='en', tweet_mode='extended').items(200)):
csvWriter.writerow([search.retweeted_status.full_text])
csvFile.close()
index.html
<iframe src="livestream.html" width=30% height=100% style="position:absolute;left:70%">
<p>Your browser does not support iframes.</p>
</iframe>
<form action="/pie" method="POST" id="nameform" class="center_text">
Keyword:
<input type="text" name="keyword"> <br><br>
<br><br>
<button type="submit" style="color:green; border:5px solid blue;border-radius: 10px; " value="Submit" onclick="loading();">Search</button>
</form>