Notice
Recent Posts
Recent Comments
Link
터칭 데이터
13주차 - 2 [프로젝트] API와 DAG 본문
1. Google Trends는 공식적으로 API를 제공하지 않는다.
2. pytrends라는 비공식 API가 존재하지만 모듈 관리가 제대로 되지 않는 상황 (가혹한 TooManyRequestsError 등)
3. 최후의 방법으로 Google Trends에서 웹 스크래핑을 하려했으나 구글측에서 bs4는 물론 Selenium을 이용한 크롤링을 원천차단
4. Network에서 Google Trends의 API를 알아내 오늘 날짜의 현재 시각 키워드를 알아내는데는 성공
(팀원분께서 도와주셨다)
5. 위의 작업을 DAG로 작성하고 Airflow를 통해 PostgreSQL(Redshift)에 적재하는데 성공
Google_trends_data_extraction.py
from airflow import DAG
from airflow.decorators import task
from airflow.providers.postgres.hooks.postgres import PostgresHook
from datetime import datetime
import requests
import json
import logging
url = "~~"
# SQL connection
def get_Redshift_connection(autocommit=True):
hook = PostgresHook(postgres_conn_id='redshift_dev_db')
conn = hook.get_conn()
conn.autocommit = autocommit
return conn.cursor()
# Getting Google Trends data using API
@task
def get_google_trends():
~~
return records
# Loading data into Database
@task
def load(schema, table, records):
logging.info("load started")
cur = get_Redshift_connection()
try:
~
except Exception as error:
~
logging.info("load done")
with DAG(
dag_id = 'google_trends_data_extraction',
start_date = datetime(2024,1,1),
catchup=False,
tags=['API'],
schedule = '0 0 * * *'
) as dag:
results = get_google_trends()
load("스키마 이름", "테이블 이름", results)
'데브코스 TIL' 카테고리의 다른 글
15주차 - 1 [Kafka와 실시간 데이터] (0) | 2024.01.22 |
---|---|
14주차 - 1 [하둡과 Spark] (0) | 2024.01.15 |
12주차 - 5 [Airflow 고급 & CI/CD] (0) | 2024.01.05 |
12주차 - 4 [Airflow 고급 & CI/CD] (0) | 2024.01.04 |
12주차 - 2 [Airflow 고급 & CI/CD] (0) | 2024.01.03 |