📝

6. 크롤러 만들기

 
notion imagenotion image
notion imagenotion image
 
10분마다 작동하는 스케줄러를 만들어보도록 하겠습니다. 아래는 sample 크롤링 페이지입니다. 앞으로 여기 페이지를 크롤링 하도록 하겠습니다. 해당 페이지는 네이버 증권 페이지에 class 명을 따르고 있습니다.
 
크롤링 방지 시스템이 있어 동일한 페이지를 아래 페이지에도 넣어두었으니 colab에서 테스트 하실 분들은 아래 URL을 사용해주세요.
 
크롤링 코드는 아래와 같습니다. 코드 최적화는 하지 않았습니다. 해당 코드가 나오게 된 주피터 노트북을 공유합니다. 자세한 내용은 무료강의인 30분 요약강좌 시즌2 크롤링 부분을 참고해주세요.
아래 소스코드를 test.py 파일에 그대로 넣어주세요.
 
import requests from bs4 import BeautifulSoup response = requests.get("http://paullab.synology.me/stock.html") response.encoding = 'utf-8' html = response.text soup = BeautifulSoup(html, 'html.parser') oneStep = soup.select('.main')[2] twoStep = oneStep.select('tbody > tr')[1:] 날짜 = [] 종가 = [] 전일비 = [] 거래량 = [] for i in twoStep: 날짜.append(i.select('td')[0].text) 종가.append(int(i.select('td')[1].text.replace(',', ''))) 전일비.append(int(i.select('td')[2].text.replace(',', ''))) 거래량.append(int(i.select('td')[6].text.replace(',', ''))) l = [] for i in range(len(날짜)): l.append({ '날짜':날짜[i], '종가':종가[i], '전일비':전일비[i], '거래량':거래량[i], }) print(l)
 
main.yml 파일은 아래와 같이 수정합니다.
name: helloGithubAction on: schedule: - cron: "*/10 * * * *" jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: 1. 파일 목록 출력 run: ls - name: 2. 설치되어 있는 패키지 확인 run: pip list - name: 3. pip 업그래이드 run: python -m pip install --upgrade pip - name: 4. 환경 설정 run: pip install -r requirements.txt - name: 5. 파이썬 실행 run: python githubactiontest.py
 
마우스를 오버해보면 아래와 같이 설명을 출력해줍니다.
notion imagenotion image
 
2개의 파일을 commit 하면 0분 10분, 20분, 30분, 40분, 50분에 크롤링을 하게 됩니다.
notion imagenotion image