티스토리 뷰

728x90
반응형

 

 

C:\Python39\site-packages\bs4\__init__.py:337: MarkupResemblesLocatorWarning: "......................." looks like a directory name, not markup. You may want to open a file found in this directory and pass the filehandle into Beautiful Soup.
  warnings.warn(
C:\Python39\site-packages\bs4\__init__.py:337: MarkupResemblesLocatorWarning: "....." looks like a directory name, not markup. You may want to open a file found in this directory and pass the filehandle into Beautiful Soup.
  warnings.warn(
C:\Python39\site-packages\bs4\__init__.py:337: MarkupResemblesLocatorWarning: "ㅋㅋㅋㅋ저두요./.." looks like a directory name, not markup. You may want to open a file found in this directory and pass the filehandle into Beautiful Soup.
  warnings.warn(
C:\Python39\site-packages\bs4\__init__.py:337: MarkupResemblesLocatorWarning: "저두요./.." looks like a directory name, not markup. You may want to open a file found in this directory and pass the filehandle into Beautiful Soup.
  warnings.warn(

위와 같은 오류를 보신 적이 있나요? 저는 검색해봐도 잘 안나와서 저게 무슨 오류인가 열심히 찾아보다가 원인을 알게되었습니다!

 

일단 위 내용은 오류가 아닙니다. Beautifulsoup 모듈(라이브러리)을 사용하신다면 가끔 보실 수 있는 경고(Warning)입니다.

먼저 일반적인 Beautifulsoup 사용법에 대해서 말해보겠습니다.

from bs4 import BeautifulSoup

html = "<h1>hello, world!</h1>"
soup = BeautifulSoup(html, "lxml")
print(soup.select("h1")[0].text)

# 'hello, world!'

BeautifulSoup 에 html 인자를 주고 어떤 파싱 방식을 사용할 것인지 설정해줘서 파싱합니다. 그리고 h1 태그 안의 텍스트 내용을 가져와서 출력해줍니다.

 

위와 같이 일반적인 경우라면 html 태그를 BeautifulSoup 함수의 첫 번째 인자로 넣어줍니다. 그럼 과연 이 경고는 언제 나오는 것일까요?

MarkupResemblesLocatorWarning: "..." looks like a directory name, not markup. You may want to open a file found in this directory and pass the filehandle into Beautiful Soup.

바로 아래의 코드의 경우에 나오는 경고입니다.

from bs4 import BeautifulSoup
html = "흠./.."
soup = BeautifulSoup(html, "lxml")

'''
C:\Python39\lib\site-packages\bs4\__init__.py:332: MarkupResemblesLocatorWarning: "흠./.." looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.
  warnings.warn(
'''

분명 html 형식이 들어올 걸 예상했지만, 이상한 문자열이 들어왔을 경우 특히 ./ 나 ../ 나 .... 와 같은 문자열이 들어왔을 때 위와 같은 경고가 나옵니다. 경고의 내용을 읽어보면 알다시피 BeautifulSoup 에서는 html 태그가 아닌 디렉토리의 경로와 같은 형태가 들어왔다고 경고를 보내주는 것입니다.

 

일반적으로 짧은 코드에서는 위와 같은 상황이 발생하지 않겠지만, 대량의 데이터를 크롤링하는 데 있어서, 작은 확률로 특정 html 태그 안에 있는 태그 안 쪽의 내용을 다시 BeautifulSoup 함수의 인자로써 사용하게 될 때 html 태그가 존재하지 않으면 위와 같은 경우가 생깁니다.

 

경고가 나오는 것에 대해서는 프로그램 로직 상 문제는 생기지 않겠지만, 만약 보기에 거슬리거나 너무 많이 발생해서 프로그램 성능이 느려지는 것 같으시면 python 에서 warning을 없애는 방법을 사용하시면 되겠습니다.

import warnings

warnings.filterwarnings("ignore", category=UserWarning, module='bs4')

위와 같이 코드를 작성하시면 bs4 모듈(라이브러리)에 대한 사용자 경고는 무시하게 됩니다.

 

 

- 끝 -

728x90
반응형
댓글