Я пытаюсь BeautifulSoup для анализа html-файлов, которые кодируются в UTF-8.Но, к сожалению, этот HTML-файл содержит несколько символов, которые не являются символами UTF-8, поэтому не отображаются правильно.Но это нормально для меня, так как я мог бы просто пропустить эти символы.
Проблема в том, что даже если я прямо указываю encodingFrom как utf-8:
soup = BeautifulSoup (html,fromEncoding='utf-8')
Получаетсяsoup.originalEncoding автоматически устанавливается по умолчанию для windows-1252.
print soup.originalEncoding
windows-1252
Я ссылался на документы BeautifulSoup, и он написан так:
Beautiful Soup tries the following encodings, in order of priority, to turn your document into Unicode:
- An encoding you pass in as the fromEncoding argument to the soup
constructor.
- An encoding discovered in the document itself
- An encoding sniffed by looking at the first few bytes of the file. If
an encoding is detected at this stage, it will be one of the UTF-*
encodings, EBCDIC, or ASCII.
- An encoding sniffed by the chardet library, if you have it installed.
- UTF-8
- Windows-1252
Кажется, он должен использовать fromEncoding, указанный мнойпадения до последнего в списке.
Вот исходный HTML-код, который я анализирую для вашей справки.