1
0

今日のpython error: UnicodeEncodeError: 'utf-8' codec can't encode characters

Last updated at Posted at 2019-12-31

昨日(20191230)の記事

今日のpython error: ModuleNotFoundError: No module named 'bs4'
https://qiita.com/kaizen_nagoya/items/f0056e5fc735b3ad5783

で、最後に出た

docker/ubuntu
# python3 wib.py
Traceback (most recent call last):
  File "wib.py", line 15, in <module>
    url = "https://ja.wikipedia.org/wiki/" + urllib.parse.quote(args[1])
IndexError: list index out of range

これは、実行時に引数を与えなかったからだった。
夜遅くの作業で、記事をあげて修了した。

朝、起きて、「IndexError: list index out of range」検索
IndexError: list index out of rangeのエラーが修正できません(初心者です)
https://teratail.com/questions/166749

元記事と
プログラムちょい替え(2)Python3: URLをコマンドライン引数で(wikipedia)
https://qiita.com/kaizen_nagoya/items/fc095b0c580a35001ea7

そのさらに元記事
wikipediaからのページからリンク一覧を取得する方法
https://qiita.com/tadaken3/items/e09ba2ede988bbacb303

を眺めるとargs[]は、コマンドラインの引数で、実行時に引数を与えていることがわかる。

昨日の夜はpythonの記述方法で頭がいっぱいで、実行時のコマンドにまで思いが至ってなかった。

docker/ubuntu
# python3 wic.py  統計
Traceback (most recent call last):
  File "wic.py", line 15, in <module>
    url = "https://ja.wikipedia.org/" + urllib.parse.quote(args[1])
  File "/usr/lib/python3.6/urllib/parse.py", line 819, in quote
    string = string.encode(encoding, errors)
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-5: surrogates not allowed

日本語ではなく英語で実行してみる。

docker/ubuntu
# python3 wic.py hazop
Traceback (most recent call last):
  File "wic.py", line 17, in <module>
    html = urlopen(url)
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0