UnicodeEncodeError: 'charmap' codec can't encode characters

后端 未结 8 630
感情败类
感情败类 2020-11-22 11:55

I\'m trying to scrape a website, but it gives me an error.

I\'m using the following code:

import urllib.request
from bs4 import BeautifulSoup

get =          


        
8条回答
  •  悲哀的现实
    2020-11-22 12:50

    In Python 3.7, and running Windows 10 this worked (I am not sure whether it will work on other platforms and/or other versions of Python)

    Replacing this line:

    with open('filename', 'w') as f:

    With this:

    with open('filename', 'w', encoding='utf-8') as f:

    The reason why it is working is because the encoding is changed to UTF-8 when using the file, so characters in UTF-8 are able to be converted to text, instead of returning an error when it encounters a UTF-8 character that is not suppord by the current encoding.

提交回复
热议问题