经过多次尝试后无法访问Twitter

2 投票
2 回答
578 浏览
提问于 2025-04-18 01:34

我在使用Windows 7系统,想通过tweepy和twitter1.14.2-python访问Twitter。但是我遇到了一些问题,无法解决。需要帮助。

TWEEPY

import tweepy

OAUTH_TOKEN = "defined here"
OAUTH_SECRET = "defined here"
CONSUMER_KEY = "defined here"
CONSUMER_SECRET = "defined here"

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_SECRET)

api = API.GetUserTimeline(screen_name="yyy")

错误:名称 'API' 未定义

TWITTER 1.14.2

import twitter
from twitter import *

tw = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,CONSUMER_KEY, CONSUMER_SECRET))
tw.statuses.home_timeline()
tw.statuses.user_timeline(screen_name="yyy")

错误:没有名为OAuth的模块

我哪里出错了?

2 个回答

1

最后我找到了用tweepy解决问题的方法

import tweepy

CONSUMER_KEY = 'Deliver here'
CONSUMER_SECRET = 'Deliver here'
OAUTH_TOKEN = 'Deliver here'
OAUTH_SECRET = 'Deliver here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_SECRET)
urapi = tweepy.API(auth)
me = urapi.me()
print me().name
print me().screen_name
print me.followers_count
for status in tweepy.Cursor(myapi.user_timeline,id="abby").items(2):
     print status.text+"/n"
2

因为在 tweepy.API 这个类里没有定义或声明 GetUserTimeline 这个方法,所以我猜你是想用 twitter.Api 这个类里的 GetUserTimeline 方法。

这个 API 是通过 twitter.Api 这个类来提供的。

要创建一个 twitter.Api 类的实例,你需要这样做:

>>> import twitter
>>> api = twitter.Api()

要用登录凭证创建一个 twitter.Api 的实例。

>>> api = twitter.Api(consumer_key='consumer_key', 
          consumer_secret='consumer_secret', access_token_key='access_token', 
          access_token_secret='access_token_secret') 

要获取某个用户的公开状态消息,其中 user 可以是这个用户的 Twitter "短名称" 或者他们的用户ID

>>> statuses = api.GetUserTimeline(user)
>>> print [s.text for s in statuses]

撰写回答