gpt4 book ai didi

python 错误 "quote_from_bytes() expected bytes"

转载 作者:行者123 更新时间:2023-11-28 18:22:03 30 4
gpt4 key购买 nike

所以我有这个代码片段,每当我尝试执行它时都会返回错误。

下面是代码,它应该根据灵活数量的字符串参数进行谷歌图片搜索。

@bot.command()
async def randomimage(*args):
"""Displays a random image of said thing"""
q = ''

for arg in enumerate(args):
q += urllib.parse.quote(arg) + '+'

f = urllib2.urlopen('http://ajax.googleapis.com/ajax/services/search/images?q=' + q + '&v=1.0&rsz=large&start=1')
data = json.load(f)
f.close()

然而,当我尝试执行它时出现此错误:

Traceback (most recent call last):  
File "Python36\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)

File "bot.py", line 39, in randomimage
q += urllib.parse.quote(arg) + '+'
File "parse.py", line 775, in quote
return quote_from_bytes(string, safe)
File "parse.py", line 800, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes

任何帮助将不胜感激

最佳答案

args 是字符串列表吗?如果是这样,您应该将它们转换为字节,以便 urllib.parse.quote 正常工作。

q += urllib.parse.quote(arg) + '+' 更改为 q += urllib.parse.quote(arg.encode('utf-8')) + '+'q += urllib.parse.quote(bytes(arg)) + '+'

关于 python 错误 "quote_from_bytes() expected bytes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44607755/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com