使用 tweepy 发送带有变量的推文

Send Tweet with Variable using tweepy

所以我正在尝试使用 tweepy 发送带有变量的推文。准确的说,整条推文都是一个变量。

代码片段:

tweet = '"If you don\'t think every day is a good day, just try missing one." - Cavett Robert\n\n Tweet URL: https://twitter.com/\nhttp://redd.it/31a5k0'
status = api.update_status(status=tweet)

当我 运行 那个时,我得到这个错误:

Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    api.update_status(status=tweet)
  File "C:\Python34\lib\site-packages\tweepy\api.py", line 193, in update_status
    )(post_data=post_data, *args, **kwargs)
  File "C:\Python34\lib\site-packages\tweepy\binder.py", line 239, in _call
    return method.execute()
  File "C:\Python34\lib\site-packages\tweepy\binder.py", line 223, in execute
    raise TweepError(error_msg, resp)
tweepy.error.TweepError: Twitter error response: status code = 403

不是因为推文太长了。共 139 个字符。

当我使用 api.update_status(status='Hello') 时,它工作得很好。只有当我使用变量时它才会中断。

我也试过以下方法:
api.update_status(status='%s' % tweet)
api.update_status(status=('%s' % tweet))
api.update_status(status='{0}'.format(tweet))
api.update_status(status=str(tweet))

现在我是 python 的新手,所以我不完全理解这个错误。有人可以告诉我为什么这不起作用以及我如何正确使用它吗?

您收到的错误代码是 HTTP 403: Forbidden 状态代码,因此您的 API 调用以某种方式被 Twitter API 禁止,而不是 tweepy

根据 Twitter API docs:

Any attempt that would result in duplication will be blocked, resulting in a 403 error. Therefore, a user cannot submit the same status twice in a row.

因此,请尝试更改您的内容,之后它可能会起作用。

推特把link(https://dev.twitter.com/overview/t.co)换行后,两个link的长推文实际上超过了140个字符。每个 link 最终都是 22 个字符。