TypeError: __init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'
TypeError: __init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'
我正在尝试通过 tweepy API 从 Twitter 获取推文,为此,我编写了以下代码,其中包含用于连接到 pyspark 的套接字,以进一步分析正在获取的推文。
代码:
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
import socket
import json
consumer_key="IvWeydhuRZoHbigNvX9wl8FbX"
consumer_secret="hv9NnWV64f4ECnK3eqJIhwQiu0SoDMtL1b9ImPJJFPwSSKleOg"
access_token="3945967939-cqy1y3J6cPcKi2fohK3qr5QSB2Z3lrOcs863tPI"
access_secret="oJIXDC37NsRU1FNP4jznCyIVwRiBjaHF9L3qsyxybkykS"
class TweetListener(Stream):
def __init__(self, csocket):
self.client_socket=csocket
def on_data(self, data):
try:
msg = json.loads(data)
print(msg('text').encode('utf-8'))
self.client_socket.send(msg('text').encode('utf-8'))
return True
except BaseException as e:
print('Error %s'%str(e))
return True
def on_error(self, status):
print(status)
return True
def send_data(c_socket):
auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)
twtr_stream = Stream(auth, TweetListener(c_socket))
twtr_stream.filter(track=['ETH'])
s = socket.socket()
host = "127.0.0.1"
port = 5000
s.bind((host,port))
print("Active port %s"%str(port))
send_data(c)
输出错误:
TypeError Traceback (most recent call last)
/tmp/ipykernel_22860/3758390964.py in <module>
----> 1 send_data(c)
/tmp/ipykernel_22860/1165989285.py in send_data(c_socket)
2 auth = OAuthHandler(consumer_key,consumer_secret)
3 auth.set_access_token(access_token,access_secret)
----> 4 twtr_stream = Stream(auth, TweetListener(c_socket))
5 twtr_stream.filter(track=['ETH'])
TypeError: __init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'
这看起来与 Stream
class 是 supposed to be used 的方式完全相反。
API 键应该作为参数传递给 Stream
基础 class 构造函数。根本不需要另外构造另一个Stream
对象,或者根本不需要处理OAuthHandler
。
最快的修补方法是:
class TweetListener(Stream):
def __init__(self, *args, csocket):
super().__init__(*args)
self.client_socket = csocket
...
...
def send_data(c_socket):
twtr_stream = TweetListener(
consumer_key, consumer_secret,
access_token, access_secret,
csocket=c_socket
)
twtr_stream.filter(track=['ETH'])
顺便说一句,我希望那些 API 密钥能很快被撤销。
我正在尝试通过 tweepy API 从 Twitter 获取推文,为此,我编写了以下代码,其中包含用于连接到 pyspark 的套接字,以进一步分析正在获取的推文。 代码:
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
import socket
import json
consumer_key="IvWeydhuRZoHbigNvX9wl8FbX"
consumer_secret="hv9NnWV64f4ECnK3eqJIhwQiu0SoDMtL1b9ImPJJFPwSSKleOg"
access_token="3945967939-cqy1y3J6cPcKi2fohK3qr5QSB2Z3lrOcs863tPI"
access_secret="oJIXDC37NsRU1FNP4jznCyIVwRiBjaHF9L3qsyxybkykS"
class TweetListener(Stream):
def __init__(self, csocket):
self.client_socket=csocket
def on_data(self, data):
try:
msg = json.loads(data)
print(msg('text').encode('utf-8'))
self.client_socket.send(msg('text').encode('utf-8'))
return True
except BaseException as e:
print('Error %s'%str(e))
return True
def on_error(self, status):
print(status)
return True
def send_data(c_socket):
auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)
twtr_stream = Stream(auth, TweetListener(c_socket))
twtr_stream.filter(track=['ETH'])
s = socket.socket()
host = "127.0.0.1"
port = 5000
s.bind((host,port))
print("Active port %s"%str(port))
send_data(c)
输出错误:
TypeError Traceback (most recent call last)
/tmp/ipykernel_22860/3758390964.py in <module>
----> 1 send_data(c)
/tmp/ipykernel_22860/1165989285.py in send_data(c_socket)
2 auth = OAuthHandler(consumer_key,consumer_secret)
3 auth.set_access_token(access_token,access_secret)
----> 4 twtr_stream = Stream(auth, TweetListener(c_socket))
5 twtr_stream.filter(track=['ETH'])
TypeError: __init__() missing 2 required positional arguments: 'access_token' and 'access_token_secret'
这看起来与 Stream
class 是 supposed to be used 的方式完全相反。
API 键应该作为参数传递给 Stream
基础 class 构造函数。根本不需要另外构造另一个Stream
对象,或者根本不需要处理OAuthHandler
。
最快的修补方法是:
class TweetListener(Stream):
def __init__(self, *args, csocket):
super().__init__(*args)
self.client_socket = csocket
...
...
def send_data(c_socket):
twtr_stream = TweetListener(
consumer_key, consumer_secret,
access_token, access_secret,
csocket=c_socket
)
twtr_stream.filter(track=['ETH'])
顺便说一句,我希望那些 API 密钥能很快被撤销。