• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

엑셀파일로 저장시 오류 문제

19.11.14 16:31 작성 조회수 1.05k

0

안녕하세요 강의를 듣고 똑같이 따라 했는데, 

파일 저장 경로에서 클릭해보니 아래처럼 뜨네요. 

인코딩이 잘못 된건지 전 강의에서 나온 에러인듯 한데 어떻게 해야할까요?에러코드는 맨아래사진처럼 나옵니다. 

from urllib.request import urlopen
from bs4 import BeautifulSoup
import openpyxl

xl_file = openpyxl.Workbook()
xl_sheet = xl_file.active

for index in range(1,6):
    res = urlopen("https://www.seeko.kr/zboard4/zboard.php?id=mainnews&page=" + str(index) + "&select_arrange=headnum&desc=asc&category=&sn=off&ss=on&sc=off&keyword=&sn1=&divpage=10")
    soup = BeautifulSoup(res, "html.parser")
    data = soup.find_all("td", "article_subject")
    for item in data:
        #print(item.get_text())
        xl_sheet.append([item.get_text()])

xl_file.save("IT.xlsx")
xl_file.close()

답변 3

·

답변을 작성해보세요.

0

Cho님의 프로필

Cho

2021.01.09

파일을 jupyter notebook에서 열지 마시고, 생성된 폴더에 가셔서 열면 됩니다.

0

9일전에 답변을 드렸는데, 이 질문만 답변이 안보여서 다시 상기만 드립니다. 위 답변부탁드립니다.

0

안녕하세요. 사실 제 맥북 환경에서는 위 코드가 정상동작을 합니다. 또한 openpyxl은 기본적으로 UTF-8을 쓰기 때문에, 별도 인코딩 처리를 필요로 하지는 않는데요. 아마도  맨 아랫줄의 See Console for more details 와 같이 상세 내용을 봐야 좀더 이해할 수 있겠지만, IllegalCharacterError Exception 라는 에러가 날 수도 있는듯 해요. 데이터에 UTF-8 인코딩으로는 조금 잘못된 데이터가 포함되는 경우가 있을 수 있습니다. 이때에는 다음과 같이 임시적으로 (볼드체로 표기하였습니다.) 코드를 변경해서 실행해보시는 것도 좋을 것 같습니다. 

물론 제 환경에서는 위의 코드나, 아래의 코드나 모두 정상동작합니다. 감사합니다.

-------

from urllib.request import urlopen

from bs4 import BeautifulSoup

import openpyxl

xl_file = openpyxl.Workbook()

xl_sheet = xl_file.active

for index in range(1,6):

    res = urlopen("https://www.seeko.kr/zboard4/zboard.php?id=mainnews&page=" + str(index) + "&select_arrange=headnum&desc=asc&category=&sn=off&ss=on&sc=off&keyword=&sn1=&divpage=10")

    soup = BeautifulSoup(res, "html.parser")

    data = soup.find_all("td", "article_subject")

    for item in data:

        #print(item.get_text())

        data = item.get_text()

        xl_sheet.append([data.strip()])

xl_file.save("IT.xlsx")

xl_file.close()