我是python的新手,我正在使用tweepy库进行twitter流式处理,我正在尝试创建一个为我处理OAth身份验证的类。
from config.config import consumer_secret, consumer_key, secret, token
import tweepy
from tweepy.auth import OAuthHandler
from tweepy import API
from tweepy import OAuthHandler
class Authentication:
"""
Class that handles the authentication for streaming
http://docs.tweepy.org/en/v3.5.0/auth_tutorial.html
"""
def __init__(self):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.secret = secret
self.token = token
def getAuthorization(self):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.secret = secret
self.token = token
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.token, self.secret)
return auth
我有另一个实现Twitter Streaming的课程
import tweepy
from tweepy.streaming import StreamListener
from tweepy import Stream
from tweepy.auth import OAuthHandler
class Listener(tweepy.StreamListener):
"""
Class that inherits from tweepy StreamListener
http://tweepy.readthedocs.io/en/v3.5.0
"""
def on_status(self, status):
print status
print(status.text)
def on_data(self, data):
print data
return True
def on_error(self, status):
if status == 420:
# returning False in on_data disconnects the stream
print status
return False
问题是当我尝试在主类上访问myStream的方法时,比如我的流过滤器,我得到以下错误:
# main class for the bigDataTweetListener project
import tweepy
from tweepy import Stream
from lib.authentication import Authentication
from lib.tweetListener import Listener
from tweepy.auth import OAuthHandler
# API Authentication
auth = Authentication()
auth.getAuthorization()
api = tweepy.API(auth)
print("authorization successful")
#Listen a Stream by topic
myStreamListener = Listener()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(track="world cup")
Traceback(最近一次调用最后一次): 文件“C:/Users/Sosa9/PycharmProjects/bigDataTweetListener/main.py”,第17行,in myStream.filter(track =“world cup”) 文件“C:\ Users \ Sosa9 \ PycharmProjects \ bigDataTweetListener \ venv \ lib \ site-packages \ tweepy \ streaming.py”,第228行,在过滤器中 self._start(异步) 文件“C:\ Users \ Sosa9 \ PycharmProjects \ bigDataTweetListener \ venv \ lib \ site-packages \ tweepy \ streaming.py”,第172行,在_start中 self._run() 文件“C:\ Users \ Sosa9 \ PycharmProjects \ bigDataTweetListener \ venv \ lib \ site-packages \ tweepy \ streaming.py”,第105行,在_run中 self.auth.apply_auth(url,'POST',self.headers,self.parameters) AttributeError:身份验证实例没有属性'apply_auth'
我不知道为什么如果我授予了创建身份验证类实例的访问权限,那么来自流类的methods.attributes是不可访问的。任何想法?
答案 0 :(得分:0)
首先,你是mixin python2和3.你应该:“print(status)”,而不是“打印状态”来使用Python3。 (打印(数据))。
其次,getAuthorization()
中的四个第一行是无用的:您已经在__init__
中声明了这些变量。所以你可以删除或评论它们:
def getAuthorization(self):
# self.consumer_key = consumer_key
# self.consumer_secret = consumer_secret
# self.secret = secret
# self.token = token
auth = OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.token, self.secret)
return auth
最后,getAuthorization()
会返回auth
,因此请替换为:
auth.getAuthorization()
by:
auth = auth.getAuthorization()