【问题标题】:Python3 check Twython Tweet less than 140 characters with len()Python3 使用 len() 检查 Twython Tweet 少于 140 个字符
【发布时间】:2015-06-21 17:46:54
【问题描述】:

我对 Python 和 Raspberry Pi 非常陌生。

我的目标:作为构建 Raspberry Pi 并学习一些 Python 的一部分,我计划构建一个气象站。作为其中的一部分,它会在推特上发布天气信息。虽然这可能不是将要使用的最终代码。这有助于我学习。这就是我发布这个问题的原因。

我已经整理了来自各种来源的代码,以便与 Twython 一起发布到 Twitter。代码(见下文)运行良好。我必须使用 python3 才不会出现 SSL 错误。

#!/usr/bin/env python3
import sys
from twython import Twython, TwythonError

#tweetStr = "Tweet goes here, limit 140 characters"
tweetStr = input("Type your Tweet: ")


#Your Twitter Application keys
apiKey = 'apiKey'
apiSecret = 'apiSecret'
accessToken = 'accessToken'
accessTokenSecret = 'accessTokenSecret'

api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)
try:
    api.update_status(status=tweetStr)
except TwythonError as Error:
    print (Error)

print ("Tweeted: ", tweetStr)

这是我喜欢的方式。要求输入。如果它有效,它会打印以筛选推文作为它有效的确认。但是,我想添加检查用户输入以验证它是否为 140 个字符或更少的功能。如果小于 141,则继续,如果超过 140,则返回错误,说明您输入的字符过多。我将只使用文本推文,没有链接。

我可以让以下内容自行工作。但是,我不确定如何让它与上述内容一起使用。 (注意:我使用

tweetStr = input("Tweet: ")
if len(tweetStr) < 10:
  print (tweetStr)
else:
  print ('too long')

我尝试了以下方法,但没有成功:

#!/usr/bin/env python3
import sys
from twython import Twython, TwythonError

#tweetStr = "Tweet goes here, limit 140 character"
tweetStr = input("Type your Tweet: ")

#Your Twitter Application keys
apiKey = 'apiKey'
apiSecret = 'apiSecret'
accessToken = 'accessToken'
accessTokenSecret = 'accessTokenSecret'

api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)

if len(tweetStr) < 15:
    try:
        api.update_status(status=tweetStr)
    except TwythonError as Error:
        print (Error)
else:
      print ('Too long use less than 141 characters')

print ("Tweeted: ", tweetStr)

任何帮助将不胜感激。也许有一种完全不同且更简单的方法可以用 Twython 做同样的事情。

【问题讨论】:

  • 您遇到了什么问题?
  • 当我尝试问题中的最后一个代码时。它提示我输入一条带有“键入您的推文:”的推文。我只是输入“测试”然后返回。我收到以下错误:Traceback (most recent call last): File "Tweet.py", line 6, in &lt;module&gt; tweetStr = input("Type your Tweet: " File "&lt;string&gt;", line 1, in &lt;module&gt; NameError: name 'test' is not defined"

标签: python-3.x twitter twython raspberry-pi2


【解决方案1】:

我自己拼凑了一个答案。

#! /usr/bin/ python3
import sys
from twython import Twython, TwythonError

#Your tweet, ask for user input
tweetStr = input('Type your Tweet: ')

#check for twitter input limit, return error if over 140
if len(tweetStr) > 140:
    print('Error! You exceeded 140 Characters. Please try again.')
    sys.exit()

# Your Twitter Application keys
apiKey = 'apiKey'
apiSecret = 'apiSecret'
accessToken = 'accessToken'
accessTokenSecret = 'accessTokenSecret'

api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)

#send your tweet or return Twython error
try:
    api.update_status(status=tweetStr)
except TwythonError as Error:
    print (Error)
#print back your tweet, likely succeeded
print ('Tweeted: ',tweetStr)

尽管我在 #! 中调用了 python3,但由于某种原因,我的 Pi 上发生了一些变化,我必须运行:

python3 filename.py

最初,我可以运行:

python filename.py

感谢收看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 2016-05-29
    • 2012-01-11
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多