【问题标题】:Send Tweet with Variable using tweepy使用 tweepy 发送带有变量的推文
【发布时间】:2015-04-03 11:56:50
【问题描述】:

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

代码sn-p:

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 新手,所以我不完全理解错误。有人可以告诉我为什么这不起作用以及如何正确使用它吗?

【问题讨论】:

  • 你是如何对 twitter 进行身份验证的?您的 API 密钥有效吗?
  • 是的@ThomWiggers。我确实说过当我不使用变量时它可以正常工作。

标签: python python-3.x twitter tweepy


【解决方案1】:

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

根据Twitter API docs

任何会导致重复的尝试都将被阻止,从而导致 403 错误。因此,一个用户不能连续两次提交相同的状态。

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

【讨论】:

  • 不是问题。稍微改了一下,还是一样的错误。如果这意味着与我的任何现有推文重复,那么我试图从中发布推文的个人资料有 0 条推文。
  • 但是,403 表示您的脚本正在尝试执行 Twitter API 不允许的操作。尝试在那里找到一些东西!
  • 我阅读了您链接的页面,但没有发现任何问题。诚然,我不知道具体要寻找什么。
【解决方案2】:

在 Twitter 包装链接后,带有两个链接的长推文实际上超过 140 个字符 (https://dev.twitter.com/overview/t.co)。每个链接最终为 22 个字符。

【讨论】:

    猜你喜欢
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2021-03-19
    • 2023-04-03
    • 2017-04-12
    • 2021-12-31
    • 2018-11-09
    相关资源
    最近更新 更多