LH 공고문 크롤링 python3 > 그누보드

본문 바로가기


사이트 내 전체검색 (구글)

그누보드

LH 공고문 크롤링 python3

페이지 정보

profile_image
작성자 세인 (192.♡.0.1)
댓글 1건 조회 3회 작성일 26-02-12 21:05

본문

[root@localhost community]# cat lh_crawl.py 

import requests

from bs4 import BeautifulSoup

import pymysql

from datetime import datetime


# --- [설정] ---

DB_CONFIG = {

    'host': 'localhost',

    'user': '아이뒤',

    'password': '패스워드~',

    'db': '디비이름',

    'charset': '캐릭터셋'

}

TABLE_NAME = 'g5_write_lh_notice'

TARGET_REGIONS = ["경기도", "수원", "용인", "성남", "광명", "화성"]


def save_to_db(title, region, pan_type, status):

    conn = pymysql.connect(**DB_CONFIG)

    now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    

    try:

        with conn.cursor() as cursor:

            # Strict Mode off 필수 필드 누락 에러 방지

            cursor.execute("SET SESSION sql_mode=''")


            # 중복 체크

            check_sql = f"SELECT COUNT(*) FROM {TABLE_NAME} WHERE wr_subject = %s"

            cursor.execute(check_sql, (title,))

            if cursor.fetchone()[0] > 0:

                print(f"⏩ 건너뜀: {title}")

                return


            # 꼭 필요한 데이터만 넣어도 나머지는 DB가 알아서 빈값으로 처리하도록.

            sql = f"""

                INSERT INTO {TABLE_NAME} 

                (wr_num, wr_reply, wr_parent, wr_subject, wr_content, wr_datetime, 

                 wr_1, wr_2, wr_3, mb_id, wr_name, wr_option, wr_ip, wr_last) 

                VALUES 

                (%s, '', 0, %s, %s, %s, %s, %s, %s, 'admin', 'LH알리미', 'html1', '127.0.0.1', %s)

            """

            wr_num = -int(datetime.now().timestamp())

            content = f"유형: {pan_type} / 지역: {region} / 상태: {status}"

            

            cursor.execute(sql, (wr_num, title, content, now, region, pan_type, status, now))

            conn.commit()

            print(f"✅ 저장됨: {title}")

    except Exception as e:

        print(f"❌ DB 저장 에러: {e}")

    finally:

        conn.close()


def fetch_lh():

    print("

댓글목록

profile_image

세인님의 댓글

세인 아이피 (192.♡.0.1) 작성일

크론으로 매일 9시에 크롤링 후 DB에 저장


회사소개 개인정보취급방침 서비스이용약관 상단으로

contact.https://open.kakao.com/o/s20mpzke
discord.https://discord.gg/rEcZHwBuAh
웹마스터:공세인 개인정보관리책임자:공세인

Copyright © quail.kr All rights reserved.