【问题标题】:Tweepy Attribute ErrorTweepy 属性错误
【发布时间】:2017-06-28 22:13:00
【问题描述】:

我是 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:“用 Python 掌握社交媒体挖掘”)。有人知道这里出了什么问题吗?

身份验证在导入的 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 

非常感谢您的任何建议!

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    在第二个文件中,函数get_twitter_client,行auth = get_twitter_auth

    您将get_twitter_auth函数保存在auth中,而不是返回值

    auth = get_twitter_auth()修复它

    【讨论】:

    • 我不能投票 b/c 我在这里的声誉太低了,我只能在自己提出问题 15 分钟后接受答案 - 但不用担心,我会验证它并尽快投票在这里是可能的:)
    • 不,这真的不重要,唯一的目标是提供帮助,点赞只是为了吸引我的同事
    猜你喜欢
    • 2015-06-02
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2019-07-09
    • 2012-08-21
    • 2017-06-04
    相关资源
    最近更新 更多