반응형

[Python 재무제표 크롤링 #4] Requests, BeautifulSoup로 크롤링(Crawling), 데이터 추출하기(Data Extraction) - 1

반응형

| Requests, BeautifulSoup 라이브러리


Requests는 웹상의 html문서를 파이썬 언어를 통해 쉽게 사용자 컴퓨터로 가져올 수 있게 하는 라이브러리입니다. 


그리고 BeautifulSoup는 가져온 HTML문서를 파싱하여 아주 쉽게 데이터를 추출할 수 있도록 해주는 파이썬 라이브러리입니다. 이 두 라이브러리를 조합하면 특정 웹사이트의 HTML문서를 쉽게 가져와서 데이터를 빠르고 쉽게 추출할 수 있죠.


두 라이브러리는 pip로 쉽게 설치가능합니다. 

pip install requests
pip install BeautifulSoup4


PyCharm을 쓰시는 분들은 해당 라이브러리를 UI로 손쉽게 설치하실 수 있습니다. 설치 방법은 여기를 참조하시면 됩니다.


▶[Python] - [Python] PyCharm에서 패키지 설치하기





| Requests로 html 문서 가져오기


html으로 문서를 가져오는 방법은 다음과 같습니다. 가져오고 싶은 URL의 주소만 명시하면 해당 웹사이트의 html을 손쉽게 가져와주죠. 여기서는 http://dataquestio.github.io/web-scraping-pages/simple.html 에서 아주 간단한 html문서를 가져오도록 하겠습니다.


import requests

page = requests.get("http://dataquestio.github.io/web-scraping-pages/simple.html")
print(page.content)

위 파이썬 스크립트를 실행시키면 해당 URL에서 가져온 html문서를 볼 수 있습니다.



b'<!DOCTYPE html>\n<html>\n    <head>\n        <title>A simple example page</title>\n    </head>\n    <body>\n        <p>Here is some simple content for this page.</p>\n    </body>\n</html>'





| BeautifulSoup로 html문서 파싱하기


BeautifulSoup로 html문서를 아주 손쉽게 파싱할 수 있습니다. 단 몇 줄이면 됩니다.

from bs4 import BeautifulSoup
soup = BeautifulSoup(page.content, 'html.parser')

BeautifulSoup라는 객체만 만들면 알아서 html 문서를 파싱해주죠. 이 객체는 html에서 유의미한 데이터들을 추출하는 하는데 사용될 겁니다. 


page.content에 저장된 html문서는 말그대로 html의 그야말로 정제되지 않은 본래 문서이기 때문에 "b' \n"와 같은 문자가 섞여 있어 보기가 어렵습니다. 이런 Raw html문서를 예쁘게 볼려면 다음과 같은 코드를 입력하면 됩니다.

print(soup.prettify())
그러면 다음과 같이 html문서가 정리됩니다.

<!DOCTYPE html>
<html>
 <head>
  <title>
   A simple example page
  </title>
 </head>
 <body>
  <p>
   Here is some simple content for this page.
  </p>
 </body>
</html>
 

html문서는 계층적 구조를 가지고 있습니다. 각 태그들이 부모 자식 관계를 가지고 구성되어 있다는 뜻입니다. 


예를 들어 위 html 문서에서는 <!DOCTYPE html> 태그는 <html>를 자식으로 가지고 있고 <html> 태그는 <head>, <body>를 가지고 구성된 것이죠.


BeautifulSoup는 이런 계층적 관계를 이용해서 이용해 데이터를 추출하는 방식사용합니다. <!DOCTYPE html> 문서의 자식 태그들을 불러오는 태그는 다음과 같습니다.

list(soup.children)
결과는 다음과 같습니다. 

['html', '\n', <html>
<head>
<title>A simple example page</title>
</head>
<body>
<p>Here is some simple content for this page.</p>
</body>
</html>]


1. 'html'은 BeautifulSoup가 자체적으로 <!DOCTYPE html>을 해석해서 html이라는 문서다 라는 정보를 리스트안에 저장한 것입니다. 

2. '\n'은 위 page.content에 들어간 원 html 데이터를 보시면 아시겠지만 현재 <!DOCTYPE html>이 가지고 있는 문자열 데이터를 나타냅니다. <tag>[문자열 데이터]</tag>에서 [문자열 데이터]를 나타내는 것입니다. 

3. 리스트의 3번째 요소인 html 문서는 <!DOCTYPE html>문서의 자식 요소들을 나타냅니다.  

BeautifulSoup는 이런 각 요소들을 자체적인 클래스 자료형을 만들어 저장합니다.
[type(item) for item in list(soup.children)]

[<class 'bs4.element.Doctype'>, <class 'bs4.element.NavigableString'>, <class 'bs4.element.Tag'>]



1. Doctype 클래스는 BeautifulSoup에서 <!DOCTPYE> 태그를 나타내기 위한 클래스입니다. 


2. NavigableString은 태그 사이에 있는 문자열 데이터를 나타냅니다


3. Tag는 말 그대로 html tag 정보를 나타내는 클래스입니다.


즉 데이터를 추출할 때는 NavigableString, 다른 태그들을 이용하려면 Tag를 보면 되겠지요.

html = list(soup.children)[2]

['\n', <head>

<title>A simple example page</title>

</head>, '\n', <body>

<p>Here is some simple content for this page.</p>

</body>, '\n']


이런 식으로요.


위의 방식과 같이 Tag 자료에 접근해서 또 여기의 자식 태그들에 접근해 나간다면 <p> 태그의 데이터를 쉽게 추출해 낼 수 있을 겁입니다.

body = list(html.children)[3]
p = list(body.children)[1]
print(p.get_text())

Here is some simple content for this page.




| python 코드 종합

import requests
from bs4 import BeautifulSoup

page = requests.get("http://dataquestio.github.io/web-scraping-pages/simple.html")
soup = BeautifulSoup(page.content, 'html.parser')
html = list(soup.children)[2]
body = list(html.children)[3]
p = list(body.children)[1]

print(p.get_text())




| 마치며


하지만 위의 방식은 여러번의 list 자료형을 중복해서 사용해야 하므로 번거로운 면이 있습니다. 그래서 BeautifulSoup에서는 html 태그 정보를 한 번에 추출해 낼 수 있는 방식과 id, class명을 이용한 접근 방식, CSS Selector를 이용한 방식을 이용하여 태그의 정보를 추출해 낼 수 있는 기능을 제공합니다.


다음 포스팅에서는 BeautifulSoup로 좀 더 쉽게 html 태그의 정보를 추출해 낼 수 있는 방법을 알아보겠습니다.


도움이 되셨다면  공감 버튼 꾹 눌러주시면 감사하겠습니다 


참고자료 : https://www.dataquest.io/blog/web-scraping-tutorial-python/

반응형

이 글을 공유하기

댓글

Designed by JB FACTORY