Python 2.7과 selenium + webdriver(chromedriver), BeautifulSoup4를 이용하여 만든 크롤러입니다. 네이버에 로그인하고, 메일 페이지에서 특정 사용자로 검색 및 안 읽은 메일만 가져오도록 필터링하여 메일 내부의 첨부파일을 특정 위치로 다운로드받도록 하여 영상을 찍어 봤습니다. 네이버의 경우 로그인할 때 의심스러운 행위를 자동으로 감지하여 캡차로 막기 때문에 이를 우회하기 위해 네이버 메인을 경유하여 로그인 페이지로 이동하고, 중간중간 랜덤한 시간동안 Sleep하는 코드를 넣어두었습니다. 소스코드는 다음과 같으며, 라이센스는 MIT 이므로 누구나 원하는 곳에 사용이 가능하지만 이 프로그램을 사용함에 있어 발생하는 모든 책임은 사용자에게 있습니다.
소스코드
NaverMailCrawler.py
Utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The MIT License | |
# | |
# Copyright (c) 2018 Sanghyeon Jeon | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
import os | |
import logging | |
import logging.handlers | |
def CreateLogger(loggerName): | |
logger = logging.getLogger(loggerName) | |
if len(logger.handlers) > 0: | |
# logger already exists | |
return logger | |
logPath = os.path.join(os.path.realpath(""), "logs", loggerName + ".log") | |
Mkdirs(logPath) | |
logger.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('[%(filename)s:%(lineno)s] %(asctime)s > %(levelname)s | %(message)s') | |
# Create Handlers | |
streamHandler = logging.StreamHandler() | |
streamHandler.setLevel(logging.INFO) | |
streamHandler.setFormatter(formatter) | |
rotatingHandler = logging.handlers.RotatingFileHandler(logPath, maxBytes=1024 * 1024 * 1024) | |
rotatingHandler.setLevel(logging.DEBUG) | |
rotatingHandler.setFormatter(formatter) | |
# Add handlers to logger | |
logger.addHandler(streamHandler) | |
logger.addHandler(rotatingHandler) | |
return logger | |
def Mkdirs(filePath): | |
dirPath = os.path.sep.join(filePath.split(os.path.sep)[:-1]) | |
if not os.path.exists(dirPath): | |
os.makedirs(dirPath) | |
def RemoveFile(target, retryCount = 0): | |
for i in range(retryCount + 1): | |
try: | |
os.remove(target) | |
except: | |
continue | |
else: | |
return True | |
return False |
'Programming' 카테고리의 다른 글
LD_PRELOAD를 이용한 가변 인자 함수 후킹 (0) | 2019.04.19 |
---|---|
[C/C++] extern "C"와 네임 맹글링 (6) | 2019.03.29 |
Visual Studio에서 보호 기법 해제하고 바이너리 빌드하기 (0) | 2018.07.18 |
[Python] PyV8을 이용한 Javascript 분석 (3) | 2018.04.27 |
[Python] 멀티프로세싱 환경에서의 logging doRollOver() Error 32 해결법 (0) | 2018.04.09 |