일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 빅데이터분석기사
- 1유형
- QGIS설치
- qgis
- 2유형
- dl
- 실기
- KNN
- 딥러닝
- 공간분석
- 성능
- gradio
- Kaggle
- fastapi
- 3유형
- K최근접이웃
- 머신러닝
- DASH
- pytorch
- 인공지능
- webserving
- CUDA
- 예제소스
- Ai
- ㅂ
- streamlit
- 캐글
- 공간시각화
- GPU
- ml 웹서빙
Archives
- Today
- Total
에코프로.AI
PostgreSQL 설치 및 데이터 저장 (Feat. Python) 본문
PostgreSQL & DBeaver 설치
PostgreSQL 다운로드 및 설치
- PostgreSQL 사이트
https://www.postgresql.org/download/windows/ - PostgreSQL 다운로드 사이트
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
아래와 같은 화면에서 os 와 지원 bit(32/64)에 맞는 버전을 다운로드 받으시면 됩니다.
- 다운받으신 exe.파일을 실행하셔서 설치하시면 됩니다.
DBeaver 다운로드 및 설치
- DBeaver 공식 사이트
https://dbeaver.io/ - DBeaver 다운로드 사이트
https://dbeaver.io/download/
아래와 같은 화면이 표시되며, Windows / Max OS X 중 선택하셔서 다운로드 받습니다.
- 다운받은 exe.파일을 실행하여 설치합니다.
- PostgreSQL 접속
파이썬 데이터 PostgreSQL에 저장
PostgreSQL - 테이블 생성
create table tbl_crawl_data (
cid serial primary key,
title varchar(255),
description TEXT,
create_at timestamp default current_timestamp
);
라이브러리 설치
%pip install requests
%pip install BeautifulSoup
%pip install psycopg2
파이썬 코드
import requests
from bs4 import BeautifulSoup
# 웹 페이지에서 데이터 크롤링
def crawl_data(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 예시로서 특정 데이터를 추출
data = []
for item in soup.find_all('div'):
title = item.find('h1').text
description = item.find('p').text
data.append([title, description])
return data
# 데이터 저장
import psycopg2
# PostgreSQL 데이터베이스에 연결
def connect_db():
conn = psycopg2.connect(
dbname = 'postgres',
user = 'postgres',
password = 'cslee',
host = 'localhost',
port = '5432'
)
return conn
# 데이터 삽입
def insert_data(conn, data):
cursor = conn.cursor()
insert_query = 'insert into cslee.tbl_crawl_data (title, description) values (%s, %s)'
cursor.executemany(insert_query, data)
conn.commit()
cursor.close()
# 전체 프로세스 실행
def main():
url = 'https://www.example.com'
data = crawl_data(url)
conn = connect_db()
insert_data(conn, data)
conn.close()
if __name__ == '__main__':
main()
데이터 저장 확인
'AI Tutorial' 카테고리의 다른 글
[Python] XML 데이터 처리관련 (0) | 2024.08.18 |
---|---|
[머신러닝] KNN(K-최근접 이웃) 알고리즘 구현 (0) | 2024.08.13 |
구글 코랩(Colab) - 사용법 (0) | 2024.07.08 |
[빅분기] 실기 - 3유형 준비 (0) | 2024.06.16 |
[빅분기] 실기 - 2유형 준비 (0) | 2024.06.16 |