크롤러 작업에 대한 이해

- 4 mins

크롤러를 이해해보자!!!


1. request란?

Markdowm Image



불가능…불가능…. 그래서 중간에서 브라우저가 해석해서 우리가 눈으로 확인할 수 있는 형태로 변환해줌.

브라우저는 어떻게 동작하는가?


2. html, css, javascript


3. dom 구조 이해하기(html)

HTML(HyperText Markup Language)라고 한다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h3>This is a Heading</h3>
    <p>This is a paragraph.</p>
</body>
</html>

This is a Heading

This is a paragraph.

Markdowm Image


4. 크롤러의 동작원리

따라서, 크롤러란 우리가 평소에 클릭 혹은 주소 입력을 통해 특정 페이지에 접근하는 것과 접근해서 원하는 데이터를 직접 드래그해서 복사하는 작업을 컴퓨터가 대신해주는????

글쓴이가 임의로 정의한 것이므로…. 정확한 정의는 아니다… 이해만 하자;;;; 그래서 우리는 파이썬의 라이브러리를 통해 서버에 정보를 요청하는 작업과 원하는 데이터를 추출하는 작업을 코드화 해볼 것이다.


5. 파이썬을 이용한 크롤러 제작.


6. 본 자료에서는 네이버 웹툰 정보를 가져오는 크롤러를 만드는 작업을 진행해볼 것이다.

(1) 라이브러리 불러오기

import requests
from bs4 import BeautifulSoup

(2) url에 요청보내고, 응답 받아오기

url = 'http://comic.naver.com/webtoon/weekday.nhn'

req = requests.get(url)
html = req.text

(3) 원하는 데이터 추출가능하도록, 응답을 BeautifulSoup로 객체화시키기

soup = BeautifulSoup(html, 'html.parser')

(4) bs의 select 메소드를 통해 원하는 태그 접근하기

webtoon_list = soup.select('#content > div.list_area.daily_all > div.col > div.col_inner > ul')
list_of_webtoon_information = []
for webtoons in webtoon_list:
    for webtoon in webtoons.find_all('div'):
        list_of_webtoon_information.append((webtoon.find('a')['href'],  webtoon.find('img')['alt'])) # 튜플로 감싸서 리스트에 append하기

(5) 사진 데이터(웹툰 썸네일) 저장하기


img_url = 'http://thumb.comic.naver.net/webtoon/597478/thumbnail/thumbnail_IMAG10_487d19d8-3547-43a0-aa94-10ef7fc94cda.jpg'
f = open('webtoon_thumbnail/{}.jpg'.format('뷰티풀 군바리'), 'wb')
img_req = requests.get(img_url).content
f.write(img_req)
f.close()

참고 파일 입출력(python)

rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora