我是python和tweepy的新手并收到一条我无法理解的错误消息:
import json
from tweepy import Cursor
from twitter_client import get_twitter_client
if __name__ == '__main__':
client = get_twitter_client()
with open('home_timeline.jsonl', 'w') as f:
for page in Cursor(client.home_timeline, count=200).pages(4):
for status in page:
f.write(json.dumps(status._json)+"\n")
运行此代码会显示以下错误消息:
Traceback (most recent call last):
File "twitter_get_user_timeline.py", line 10, in <module>
for page in Cursor(client.home_timeline, count=200).pages(4):
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/cursor.py", line 49, in __next__
return self.next()
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/cursor.py", line 108, in next
data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/binder.py", line 239, in _call
return method.execute()
File "/home/projects/webscraping/testEnv/lib/python3.5/site-packages/tweepy/binder.py", line 174, in execute
auth = self.api.auth.apply_auth()
AttributeError: 'function' object has no attribute 'apply_auth'
由于这非常深入Tweepy,我无法真正理解代码中的错误在哪里(代码来自书:Marco Bonzanini:&#34;掌握使用Python的社交媒体挖掘&#34;)。有没有人知道这里出了什么问题?
验证在导入的twitter_client中完成。那里的代码是:
import os
import sys
from tweepy import API
from tweepy import OAuthHandler
def get_twitter_auth():
"""Setup Twitter authentication.
Return: tweepy.OAuthHandler object
"""
try:
consumer_key = os.environ['TWITTER_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_CONSUMER_SECRET']
access_token = os.environ['TWITTER_ACCESS_TOKEN']
access_secret = os.environ['TWITTER_ACESS_SECRET']
except KeyError:
sys.stderr.write("TWITER_* environment variables not set\n")
sys.exit(1)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
return auth
def get_twitter_client():
"""Setup Twitter API client
Return: tweepy.API object
"""
auth = get_twitter_auth
client = API(auth)
return client
非常感谢任何建议!
答案 0 :(得分:1)
在第二个文件中,功能get_twitter_client
,行auth = get_twitter_auth
。
您正在get_twitter_auth
中保存auth
功能,而不是返回值。
使用auth = get_twitter_auth()