inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

파이썬 입문 및 웹 크롤링을 활용한 다양한 자동화 어플리케이션 제작하기

파이썬으로 XML 데이터 다루기

이렇게 코딩했더니

361

작성자 없음

작성한 질문수 0

0

콘솔창에 이렇게 뜨는데  utf-8 이쪽에서 뭔가 에러가난거같은데 어떻게해야하죠?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sys
import io
import urllib.request as req
from bs4 import BeautifulSoup
import os.path #os.path 내에는 경로반환, 경로추출 등 파일/디렉토리 경로와 관련된 많은 함수를 제공해준다.
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')
#다운로드 urll
url = "http://www.weather.go.kr/weather/lifenindustry/sevice_rss.jsp"
savename = "c:/section4/forecast.xml"
if not os.path.exists(savename):
req.urlretrieve(url, savename)
#BeautifulSoup 파싱
xml = open(savename, 'r', encoding="utf-8").read()
soup = BeautifulSoup(xml, 'html.parser')
#지역확인
for location in soup.find_all("location"):
loc = location.find("city").string
print(loc)

Python - section4-1.py:26
Traceback (most recent call last):
  File "C:\section4\section4-1.py", line 19, in <module>
    xml = open(savename, 'r', encoding="utf-8").read()
  File "C:\Users\user\anaconda3\envs\section4\lib\codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 118: invalid start byte
[Finished in 0.152s]

python 웹-크롤링

답변 2

0

MIKE.aeon

파일 읽으실때, 원본파일을 보시고 euc-kr 이면 아래 처럼 인코딩을 수정하시면 됩니다.

xml = open(savename, 'r', encoding='euc-kr').read()

같은 사이트인데, url  주소에 따라서 euc-kr 인곳도 있고, utf-8 인곳도 있습니다.

0

좋은사람

안녕하세요.

현재 아래 주소가 변경된 것 같은데 확인 한 번 해보겠습니다.

http://www.weather.go.kr/weather/lifenindustry/sevice_rss.jsp

현재 예제에서 error 발생

0

374

3

유튜브 동영상 다운로드

0

1458

2

Atom 에디터 관련

0

343

1

위시켓 폼데이터

0

276

1

스케줄러 사용 관련 질문 드립니다

0

632

1

selenium 에러

0

433

1

Progress bar 쓰레드 관련

0

493

1

Install Package 관련 문의

0

336

1

tkinter 샘플 코드 실행 오류 건

0

1275

1

4-7-6 네이버 & 카카오 주식 정보 가져오기

0

383

1

네이버자동로그인_by_selenium

0

879

1

위시캣 로그인 처리 및 크롤링 질문

0

352

1

2-8-1 네이버이미지 크롤링 질문

1

609

3

li:nth-of-type 질문

0

357

2

에러가 뜨는데 잘 모르겠어요ㅠ

0

404

2

Install Packages 항목이 안보이는 이유가 뭘까요?

0

404

2

환경변수 Path 설정 방법

0

636

1

웹 브라우저 없는 스크랩핑 및 파싱 실습(1) - 인프런

0

339

1

웹 브라우저 없는 스크랩핑 및 파싱 실습(1) -git주소

0

484

3

download2-8-1. py질문

0

208

1

ip 차단 당하는 거 같은데 아무리 랜덤주고, sleep 줘도 안 되는데 다른 방법 더 있을까요??

0

647

1

인프런 환경이 바뀌어서 제나름대로 하는데

0

199

1

다시올려주신 예제파일로하는데

0

192

1

아직도 에러가뜨나보네요?

0

586

1