因此,我想做的是从Twitters API直播推文:仅用于主题标签“Brexit”,仅限英语,以及特定数量的推文(1k - 2k)。
到目前为止,我的代码将实时流式传输推文,但无论我以何种方式修改它,我要么最终忽略计数并无限期地流式传输,要么我得到错误。如果我将其更改为仅流式传输特定用户的推文,则计数函数有效,但它会忽略主题标签。如果我为给定的主题标签流式传输所有内容,它将完全忽略计数。我在尝试修复它方面已经相当不错,但我缺乏经验,并且真的用它撞上了砖墙。
如果我能得到一些帮助,如何同时勾选所有这些框,将不胜感激!到目前为止,下面的代码将无限期地流式传输“英国脱欧”推文,因此忽略计数= 10
由于我玩它,代码的底部有点混乱,抱歉:
import numpy as np
import pandas as pd
import tweepy
from tweepy import API
from tweepy import Cursor
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import Twitter_Credentials
import matplotlib.pyplot as plt
# Twitter client - hash out to stream all
class TwitterClient:
def __init__(self, twitter_user=None):
self.auth = TwitterAuthenticator().authenticate_twitter_app()
self.twitter_client = API(self.auth)
self.twitter_user = twitter_user
def get_twitter_client_api(self):
return self.twitter_client
# Twitter authenticator
class TwitterAuthenticator:
def authenticate_twitter_app(self):
auth = OAuthHandler(Twitter_Credentials.consumer_key, Twitter_Credentials.consumer_secret)
auth.set_access_token(Twitter_Credentials.access_token, Twitter_Credentials.access_secret)
return auth
class TwitterStreamer():
# Class for streaming and processing live Tweets
def __init__(self):
self.twitter_authenticator = TwitterAuthenticator()
def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
# this handles Twitter authentication and connection to Twitter API
listener = TwitterListener(fetched_tweets_filename)
auth = self.twitter_authenticator.authenticate_twitter_app()
stream = Stream(auth, listener)
# This line filters Twitter stream to capture data by keywords
stream.filter(track=hash_tag_list)
偶然的你
长风秋雁
相关分类