Python 3 UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d

后端 未结 1 1041
广开言路
广开言路 2020-12-03 02:39

I want to make search engine and I follow tutorial in some web. I want to test parse html

from bs4 import BeautifulSoup

def parse_html(filename):
    \"\"         


        
相关标签:
1条回答
  • 2020-12-03 03:43

    In Python 3, files are opened as text (decoded to Unicode) for you; you don't need to tell BeautifulSoup what codec to decode from.

    If decoding of the data fails, that's because you didn't tell the open() call what codec to use when reading the file; add the correct codec with an encoding argument:

    with open(filename, encoding='utf8') as infile:
        html = BeautifulSoup(infile, "html.parser")
    

    otherwise the file will be opened with your system default codec, which is OS dependent.

    0 讨论(0)
提交回复
热议问题