Skip to content

Instantly share code, notes, and snippets.

@nomadekr
Last active December 8, 2017 10:11
Show Gist options
  • Save nomadekr/7ec5975738cdc27ee2ff5c82c7b0499a to your computer and use it in GitHub Desktop.
Save nomadekr/7ec5975738cdc27ee2ff5c82c7b0499a to your computer and use it in GitHub Desktop.
Python Korea 2017년 12월 세미나 - Azure Functions을 활용한 파이썬 크롤링 스케줄링
  • nuget 에서 지원하는 파이썬 3.6을 설치해봅시다.
  • 함수 앱 > 플랫폼 기능 > 고급도구 (KUDU) > Debug Console > CMD
  • tools 디렉토리에 파이썬3 설치
CMD> nuget.exe install -Source https://www.siteextensions.net/api/v2/ -OutputDirectory D:\home\site\tools python361x64
  • Azure Function에서 접근가능한 경로에 Python3 이동
CMD> mv /d/home/site/tools/python361x64.3.6.1.3/content/python361x64/* /d/home/site/tools/
  • 지금부터 실행되는 Azure Function은 d:\home\site\tools\python 을 통해 실행됩니다.

  • 필요한 파이썬 팩키지도 설치

CMD> d:/home/site/tools/python -m pip install requests beautifulsoup4
  • 버전 확인
CMD> d:/home/site/tools/python --version
Python 3.6.1
import os
import json
import time
import requests
from bs4 import BeautifulSoup
def get_realtime_keywords():
'네이버 검색어 크롤링을 하고 ...'
html = requests.get('https://www.naver.com/').text
soup = BeautifulSoup(html, 'html.parser')
tags = soup.select('.PM_CL_realtimeKeyword_rolling_base .ah_k')
keywords = [tag.text for tag in tags]
return keywords
def insert(partition_key, row_key, **kwargs):
'파이썬 기본문법만으로 Azure Table NoSQL에 INSERT를 합니다.'
doc = dict(
PartitionKey=partition_key,
RowKey=row_key,
**kwargs)
tablePath = os.environ['tablePath']
with open(tablePath, 'wt', encoding='utf8') as f:
json.dump(doc, f)
if __name__ == '__main__':
# 키워드를 긁어와서
keywords = get_realtime_keywords()
# Azure Table에 추가
insert('naver_realtime_keywords', int(time.time()), keywords=keywords)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment