UnicodeEncodeError: 'charmap' codec can't encode characters

后端 未结 8 706
感情败类
感情败类 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:43

    While saving the response of get request, same error was thrown on Python 3.7 on window 10. The response received from the URL, encoding was UTF-8 so it is always recommended to check the encoding so same can be passed to avoid such trivial issue as it really kills lots of time in production

    import requests
    resp = requests.get('https://en.wikipedia.org/wiki/NIFTY_50')
    print(resp.encoding)
    with open ('NiftyList.txt', 'w') as f:
        f.write(resp.text)
    

    When I added encoding="utf-8" with the open command it saved the file with the correct response

    with open ('NiftyList.txt', 'w', encoding="utf-8") as f:
        f.write(resp.text)
    

提交回复
热议问题