【问题标题】:Error with tweepy API posting text with update_statustweepy API 使用 update_status 发布文本时出错
【发布时间】:2017-12-31 11:28:27
【问题描述】:

我正在使用 tweepy 开发一个基于 Python 的 twitter bot .为了测试,我制作了 2 个文件,它们的导入和 Python 2.7 都使用 Python 3 打印功能。一个有一个静态的、设置的 speedtest 结果,以允许我测试 Tweepy 方面,而另一个运行 speedtest 并将其写入同名变量。

真正的版本是

while True:
testresraw = subprocess.check_output('speedtest --no-upload', shell=True) #Test speed and get output
testresnone,testressplit = testresraw.split('Download: ')#Remove text before the download speed
testresint,testresnone = testressplit.split('Mbit/s')#Remove text after download speed
float(testresint)#Make sure download speed is a number
if testresint<float(10.0):
    statustext= ('My download speed is currently %d MB/s, which is less than the 10 we pay @CenturyLink for' % (testresint))
    api.update_status(status=statustext)
    time.sleep(2700)
else:
    time.sleep(2700)

测试版是

testresint = 1
if testresint<float(10.0):
    statustext = ('My download speed is currently %d MB/s, which is less than the 10 we pay @CenturyLink for' % (testresint))
    api.update_status(status=statustext)
    time.sleep(2700)
else:
    time.sleep(2700)

只有测试版有效,我不知道为什么。

编辑:我放置了一些print 函数来向我展示它在哪里搞砸了,事实证明,在判断testresint 错误地不小于10 之后,它会进入else 语句。我按照建议在 10.0 之前删除了 float。我仍然无法弄清楚出了什么问题。

【问题讨论】:

  • 您正在重置testresnone 中的testresint,testresnone = testressplit.split('Mbit/s')。那是对的吗?此外,不需要在10.0 上调用float()。
  • 是的,testresnone 是一个占位符,允许我只取子流程输出中我需要的部分,谢谢。
  • 在上一个语句中,您将testresraw.split('Download: ')[0] 分配给testresnone,然后您将其更改为testressplit.split('Mbit/s')[1],所以我想知道是否有必要这样做。
  • 是的,为了确保它没有被拆分成一个列表,而是两个单独的字符串。

标签: python python-2.7 twitter automated-tests tweepy


【解决方案1】:

我设法通过将if testresint&lt;float(10.0) 更改为if float(testresint)&lt;float(10.0) 来修复它。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2022-11-19
    • 2013-10-20
    • 2015-10-16
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    相关资源
    最近更新 更多