2017-06-17 182 views
0

私はこのコードスニペットを実行しようとするとエラーを返します。pythonエラー "quote_from_bytes()expected bytes"

コードは以下のとおりです。柔軟な量の文字列引数に基づいてGoogle画像検索を行うことになっています。

@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 

すべてのヘルプは

答えて

0

args文字列のリストですいただければ幸いですか!もしそうなら、それらをurllib.parse.quoteが正しく動作するようにバイトに変換する必要があります。そうした後q += urllib.parse.quote(arg.encode('utf-8')) + '+'またはq += urllib.parse.quote(bytes(arg)) + '+'

+0

変更q += urllib.parse.quote(arg) + '+'、私は新しいエラーを取得し、はAttributeErrorは:「タプル」オブジェクトが属性「エンコード」 –

+0

を持っていないだけで、ここで他の問題に気づきました。 'for arg in enumerate(args):'は 'argsのargに対して'でなければなりません。また、(デバッグやその他の理由で)列挙を使用する必要がある場合は、 'for _、arg in enumerate(args)' –

関連する問題