tweepy.error.TweepError:[{u'message':u'Text参数丢失。','u'code':38}}

时间:2016-09-07 10:07:48

标签: python-2.7 tweepy

我正在使用tweepy twitter api for python,在使用它时我遇到了一些错误, 我无法使用send_direct_message(user/screen_name/user_id, text)此方法

这是我的代码: -

import tweepy
consumer_key='XXXXXXXXXXXXXXXXX'
consumer_secret='XXXXXXXXXXXXXXXXX'
access_token='XXXXXXXXXXXXXXXXX'
access_token_secret='XXXXXXXXXXXXXXXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

API = tweepy.API(auth)

user = API.get_user('SSPendse')
screen_name="CJ0495"
id = 773436067231956992
text="Message Which we have to send must have maximum 140 characters, If it is Greater then the message will be truncated upto 140 characters...."
# re = API.send_direct_message(screen_name, text)
re = API.send_direct_message(id, text)
print re

出现以下错误: -

Traceback (most recent call last):
  File "tweetApi.py", line 36, in <module>
    re = API.send_direct_message(id, text)
  File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 245, in _call
    return method.execute()
  File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 229, in execute
    raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{u'message': u'Text parameter is missing.', u'code': 38}]

我将犯错误...... ???

我还有另外一个与tweepy有关的问题,如何移动到第二页或在以下代码中获得更多关注者

i=1
user = API.get_user('Apple')
followers = API.followers(user.id,-1)
for follower in followers:
    print follower,'\t',i
    i=i+1

如果我运行代码我只有5000个关注者但是如果我使用user.followers_count它会给 362705 关注者(这个号码可能会在您检查时发生变化)我怎样才能看到剩下的粉丝

谢谢你...... :)

1 个答案:

答案 0 :(得分:1)

要解决第一个错误,请将re = API.send_direct_message(id, text)替换为re = API.send_direct_message(id, text=text)。仅当您将消息作为命名参数提供时,此功能才有效。这里需要的参数名称是“text”,因此您可能希望更改变量名称以避免混淆。另外,我刚尝试过,因为你发送的是直接信息,而不是推文,它会被截断为只有前140个字符。

对于你的第二个问题,这应该可以解决问题,如here所述:

followers = []
for page in tweepy.Cursor(API.followers, screen_name='Apple').pages():
    followers.extend(page)
    time.sleep(60) #this is here to slow down your requests and prevent you from hitting the rate limit and encountering a new error
print(followers)