Python: urllib.quote のインポート 質問する

Python: urllib.quote のインポート 質問する

を使いたいのですurllib.quote()が、python (python3) がモジュールを見つけられません。次のようなコードがあるとします。

print(urllib.quote("châteu", safe=''))

urllib.quote をインポートするにはどうすればいいですか?

import urllibまたはimport urllib.quote両方を与える

AttributeError: 'module' object has no attribute 'quote'

私が困惑しているのはurllib.requestimport urllib.request

ベストアンサー1

Python 3.xでは、インポートする必要がありますurllib.parse.quote:

>>> import urllib.parse
>>> urllib.parse.quote("châteu", safe='')
'ch%C3%A2teu'

によるとPython 2.xurllibモジュールのドキュメント:

注記

このurllibモジュールは Python 3 で複数の部分に分割され、 、 、 に名前が変更されurllib.requestましurllib.parseurllib.error

おすすめ記事