Post

자연어처리 데이터 준비

대학교 과제 준비용
자연어 처리 작업을 하기 전에 필요한 데이터 크롤링 작업 준비

크롤링이란

 크롤링은 웹 페이지를 그대로 가지고 와서 데이터를 추출해 내는 것을 의미한다.

크롤링을 하는 방법?

 자연어 처리 사전작업을 하기 위해(전처리) 학교에서 파이썬을 사용하였기에 나는 파이썬을 이용해, Beautiful Soup, selenium를 사용하겠다.

selenium, Beauriful soup 사용법


무엇을 크롤링 할 것인가?

 무엇을 크롤링 해야하는고.

  1. 뉴스 comment?
  2. 뉴스 본문?
  3. 네이버 지식인?
  4. 블로그 comment?
  5. 나무위키 정보?

 너무 많은 종류가 있고 크롤링 할 때 불러올 엘리먼트가 전부 다르기에 하나를 골라 그에 맞는 코드를 작성해야 한다.

뉴스 코멘트 크롤링

jtbc comment 크롤링

네이버 주식토론방 크롤링

네이버 주식토론방 크롤링

이미지 다운로더

 아래 코드는 내가 심심풀이로 만들어본 이미지 다운로더이다. 하지만 텀프로젝트를 위해서는 글을 긁어와야한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    import os
    import requests

    def img_crawler(url_link, url_name, file_name):
        file_type = "img"
        for link, name in zip(url_link, url_name):
            print("다운로드중 : " + link)
            r = requests.get(link)
            dir_now = os.path.dirname(os.path.abspath(__file__))
            file_check(file_name, file_type)
            with open(f'{dir_now}/{file_type}/{file_name}/{name}.jpg', "wb") as outfile:
                outfile.write(r.content)
        print("다운로드 성공\n")

    def file_check(file_name, file_type):
        print("파일 체크 시작")
        path_file_type = f".\\{file_type}\\"
        if not os.path.isdir(path_file_type):
            os.mkdir(path_file_type)
            print("기본 파일 생성")
        path_file = f".\\{file_type}\\{file_name}\\"
        if not os.path.isdir(path_file):
            os.mkdir(path_file)
            print(f"{file_type} 파일 생성")
This post is licensed under CC BY 4.0 by the author.