@Doremi_chan

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

“ascii”codec can’t encode charactersエラーについて

解決したいこと

“ascii”codec can’t encode charactersエラーについて

Pythonista3でスクレイピングの練習問題に取り組んでいます。
解決方法を教えて下さい。

発生している問題・エラー

import requests
url ="https://www.ymori.com/books/python2nen/test1.html"
response = requests.get(url)
response.encoding = response.apparent_encoding
filename= "download.txt"
with open(filename, mode="w") as f:
f.write(response.text)

を実行したところ

”ascii” codec can’t encode characters in position 72-73
(line7)

が発生しました。

自分で試したこと

様々な解説サイトにて、書かれていることを追記するのですが、なかなか読み込めず…自分のやり方が悪いのではないかと考え、質問するに至りました。

0 likes

1Answer

ファイルに日本語が含まれているためエラーになっています。

import codecs

with codecs.open(filename, "w", "utf-8")

ではだめでしょうか。

0Like

Your answer might help someone💌