귀하의 코드가 파손 참조 : u.read()
반환 bytes
개체를. 그대로
>>> str(b'\xe2\x86\x90')
"b'\\xe2\\x86\\x90'"
를 어느 디스크에 바이트를 저장 :
import urllib.request
urllib.request.urlretrieve('http://stackoverflow.com', 'so.html')
당신이 그것을 원하지 않아요 -
str(bytes_object)
는 오브젝트의 캐릭터
표현을 (어떻게 보이는지 바이트 문자)를 반환
또는 바이너리 모드로 파일을 엽니 다 'wb'
를 수동으로 저장 :
import shutil
from urllib.request import urlopen
with urlopen('http://stackoverflow.com') as u, open('so.html', 'wb') as file:
shutil.copyfileobj(u, file)
또는 유니 코드 바이트로 변환 및 저장 원하는 인코딩을 사용하여 디스크에 저장하십시오.
import io
import shutil
from urllib.request import urlopen
with urlopen('http://stackoverflow.com') as u, \
open('so.html', 'w', encoding='utf-8', newline='') as file, \
io.TextIOWrapper(u, encoding=u.headers.get_content_charset('utf-8'), newline='') as t:
shutil.copyfileobj(t, file)
출처
2015-01-23 14:04:08
jfs
기호로 무엇을 의미하는지 모르므로 'unicode()'함수를 사용해보십시오. –