1

我正在尝试剥离字符b,'()。我遇到的问题是它说 TypeError 'str' 不支持缓冲区接口。

以下是此代码的相关部分:

import urllib3
def command_uptime():

    http = urllib3.PoolManager()
    r = http.request('GET', 'https://nightdev.com/hosted/uptime.php?channel=TrippedNW')
    rawData = r.data
    liveTime = bytes(rawData.strip("b,\'()", rawData))

    message = "Tripped has been live for: ", liveTime
    send_message(CHAN, message)
4

2 回答 2

3

你所拥有的是二进制数据。它不是一个字符串。您需要先对其进行解码。

此外,您不需要rawData在 strip 方法中传递给自身。

import urllib3

def command_uptime():

    http = urllib3.PoolManager()
    r = http.request('GET', 'https://nightdev.com/hosted/uptime.php?channel=TrippedNW')
    strData = r.data.decode('utf-8')
    liveTime = strData.strip("b,\'()")

    message = "Tripped has been live for: %s" % liveTime
    print(message)

command_uptime()

另请注意,您的message变量是元组而不是字符串。我不知道是否send_message期待这个。我将其格式化为单个字符串。

于 2015-05-21T02:08:01.333 回答
0

只需删除第二个参数。

import urllib3
def command_uptime():

    http = urllib3.PoolManager()
    r = http.request('GET', 'https://nightdev.com/hosted/uptime.php?channel=TrippedNW')
    rawData = r.data
    liveTime = bytes(rawData.strip("b,'()"))

    print("Tripped has been live for: %s" % liveTime)


command_uptime()

输出:

Tripped has been live for:  1 hour, 18 minutes
于 2015-05-21T02:19:21.463 回答