Beautiful Soup과 Python 2.6.5를 사용하여 스칸디나비아 문자로 웹 사이트에서 텍스트와 HTML을 추출하려고합니다. 실행될 때아름다운 스프와 문자 인코딩
html = open('page.html', 'r').read()
soup = BeautifulSoup(html)
descriptions = soup.findAll(attrs={'class' : 'description' })
for i in descriptions:
description_html = i.a.__str__()
description_text = i.a.text.__str__()
description_html = description_html.replace("/subdir/", "http://www.domain.com/subdir/")
print description_html
그러나, 프로그램은 다음과 같은 오류 메시지와 함께 실패 : 그 어떤 도움 있다면
Traceback (most recent call last):
File "test01.py", line 40, in <module>
description_text = i.a.text.__str__()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 19: ordinal not in range(128)
입력 페이지, ISO-8859-1로 인코딩 된 것으로 보인다. BeautifulSoup(html, fromEncoding="latin-1")
으로 올바른 소스 인코딩을 설정해 보았지만 도움이되지 않았습니다.
2011 년입니다. 그리고 나는 사소한 문자 인코딩 문제로 씨름하고 있습니다.이 모든 것에 대한 정말 간단한 해결책이 있다고 생각합니다.
'i.a.text.encode ('latin-1')'매우 효과적입니다. 감사합니다! –