[英]Expected string or buffer Python
I'm really new to python, it's the first time I'm writing it actually. 我真的是python新手,这是我第一次真正编写它。 And I'm creating a program that gets the number of views on a Twitch.tv stream, but I'm getting an error Expected string or buffer Python when I'm calling this function
而且我正在创建一个程序,该程序获取Twitch.tv流上的视图数,但是在调用此函数时出现错误期望字符串或缓冲区Python
def getURL():
output = subprocess.Popen(["livestreamer", "twitch.tv/streamname", "-j"], stdout=subprocess.PIPE).communicate()[1]
return json.loads(output)["streams"]["worst"]["url"]
I'm calling the function from here: 我从这里调用函数:
urls = []
urls.append(getURL())
What am I doing wrong? 我究竟做错了什么? I've been trying to figure this one out for ages... And if anybody knows how to fix this, I'd be the happiest man alive ;)
我一直在试图弄清楚这个问题……如果有人知道如何解决这个问题,我将是最活着的最幸福的人;)
Thanks in advance. 提前致谢。
EDIT: 编辑:
This is all the code I have. 这就是我所有的代码。
import requests
import subprocess
import json
import sys
import threading
import time
urls = []
urlsUsed = []
def getURL():
output = subprocess.Popen(["livestreamer", "twitch.tv/hemzk", "-j"], stdout=subprocess.PIPE).communicate()[1]
return json.loads(output)["streams"]["worst"]["url"]
def build():
global numberOfViewers
urls.append(getURL())
And I'm getting the error at return json.loads(output)["streams"]["worst"]["url"] 而且我在返回json.loads(output)[“ streams”] [“ worst”] [“ url”]时遇到了错误
Change 更改
output = subprocess.Popen(["livestreamer", "twitch.tv/streamname", "-j"],
stdout=subprocess.PIPE).communicate()[1]
to 至
output = subprocess.Popen(["livestreamer", "twitch.tv/streamname", "-j"],
stdout=subprocess.PIPE).communicate()[0]
(Change 1 to 0). (将1更改为0)。
More detailed explanation of your problem follows. 有关您的问题的详细说明如下。
Popen.communicate
returns a tuple stdout, stderr
. Popen.communicate
返回一个元组stdout, stderr
。 Here, it looks like you are interested in stdout
(index 0), but you are picking out stderr
(index 1). 在这里,您似乎对
stdout
(索引0)感兴趣,但是您正在挑选stderr
(索引1)。 stderr
is None
here because you have not attached a pipe to it. stderr
在此处为None
,因为您尚未将管道连接到该管道。 You then try to parse None
using json.loads()
which expected a str
or a buffer
object. 然后,您尝试使用
json.loads()
解析None
,该str
需要一个str
或一个buffer
对象。
If you want stderr
output, you must add stderr=subprocess.PIPE
to your Popen
constructor call. 如果要
stderr
,则必须将stderr=subprocess.PIPE
添加到Popen
构造函数调用中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.