본문 바로가기

Programming Language/Python

Beautifulsoup을 이용한 HTML 파싱 및 크롤링

Python을 이용한 공공데이터포털의 파일데이터 목록 크롤링

 

BeautifulSoup ?

: HTML 파싱 라이브러리, 기본적으로 UTF-8 인코딩 방식이지만 XML 파싱도 가능하다.

 

BeautifulSoup4 설치

  • C:\Users\skybl\AppData\Local\Programs\Python\Python37-32\Scripts
  • 관리자 CMD 실행, beautifulsoup4 설치 : pip install beautifulsoup4

 

BeautifulSoup  기본 설정

▶  package import

from bs4 import BeautifulSoup

 

urllib을 통해 웹에 있는 소스 가져오기

url에 원하는 URL 추가

import urllib.request
import urllib.parse


html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')

- urllib.requset.urlopen() 함수는 웹 서버에 정보를 요청한 후, 돌려받은 응답을 저장하여 '응답 객체(HTTPResponse)'를 반환한다.

- 반환된 응답 객체의 read() 메서드를 실행하여 웹 서버가 응답한 데이터를 바이트 배열로 읽어 들인다.

 

공공데이터포털 파싱 및 크롤링


 URL 주소는 공공데이터포털 페이지 소스보기를 통해 얻어왔다. 현재는 파일데이터에 한해서만 크롤링이 가능하다. OPEN API 또는 표준 데이터 목록을 크롤링을 하고 싶으면 index 뒤에 원하는 데이터 타입을 입력하면 된다. 공공데이터포털의 경우 키워드를 검색하면 한 페이지 당 10개의 리스트(countPerPage)가 검색된다.

이를 이용해서 데이터 이름, URL 주소, 상세 설명을 파싱 및 크롤링 해본다.

import urllib.request
import urllib.parse
from bs4 import BeautifulSoup


plusUrl = urllib.parse.quote_plus(input('검색어 입력:'))
# 사용자가 원하는 키워드 입력

pageNum=1


i = input('몇페이지 크롤링?\n')
lastPage = int(i)

while pageNum < lastPage + 1:


    url = f'https://www.data.go.kr/search/index.do?index=DATA&currentPage={pageNum}&countPerPage=10&sortType=VIEW_COUNT&data-order="DESC"&query={plusUrl}'
	

    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')


    for j in soup.find_all('div', class_='data-item'):
        tag1 = j.find('div',attrs={'class':'data-title'}).find('a')
        tag2 = j.find('div',attrs={'class':'data-desc'})
        tag3 = tag1['href']
        url1 = f'https://www.data.go.kr{tag3}'

        html1 = urllib.request.urlopen(url1).read()
        soup1 = BeautifulSoup(html1, 'html.parser')
        tag4 = soup1.find('a', {'id': 'url-in-detail-btn'})
        print(tag4)

        print('--------------------')
        # tag1 중 text만 추출(.text)하고, 왼쪽의 공백을 제거(.lstrip())한다
        print(tag1.text.lstrip())
        print(url1)
        print(tag2.text.lstrip())


    pageNum += 1

for j in soup.find_all('div', class='data-item'):

- 리스트들은 div 태그 내에서 data-time 이라는 class 명을 가지고 있다. 총 10개의 리스트가 있기 때문에, for 문을 이용하여 각 리스트의 title, href, desc를 출력한다.

 

tag1 = j.find('div',attrs={'class':'data-title'}).find('a')

 - <div class="data-item"> 내에 존재하는 <div class="data-title"> 를 찾고, 그 안에 a 태그를 찾는다.

 

tag2 = j.find('div',attrs={'class':'data-desc'})

 - <div class="data-item"> 내에 존재하는 <div class="data-desc"> 를 찾는다.

 

tag3 = tag1['href']

url1 = f'https://www.data.go.kr{tag3}'

  - tag1'href' 태그를 찾고, URL 형태로 만들어 준다.

[실행 결과]

 

※ URL 뒤에 jsessionid 가 같이 출력되는 경우 해결 방법을 찾아야 함.

 

참고:

https://twpower.github.io/84-how-to-use-beautiful-soup

https://victorydntmd.tistory.com/245

https://toentoi.tistory.com/43

 

[Python 크롤링] BeautifulSoup을 이용한 HTML 파싱 및 크롤링 ( feat. 셀레니움 )

BeautifulSoup BeautifulSoup은 HTML 파싱 라이브러리로서, 몇 개의 메서드만으로 DOM 추출이 가능합니다. 기본적으로 UTF-8 인코딩 방식이지만 CP949도 지원하며, HTML뿐만 아니라 XML 파싱도 가능합니다. 저는 I..

victorydntmd.tistory.com

 

[Python] 웹 크롤링에 사용하는 Beautiful Soup(뷰티플 수프) 사용법과 예제

Practice makes perfect!

twpower.github.io