개발

파이썬 설치

pip install python-wordpress-xmlrpc requests beautifulsoup4 lxml –break-system-packages

오라클 클라우드 우분투에서 pip로 설치시 에러뜰때.

python3 -c “from wordpress_xmlrpc import Client; import bs4; import requests; print(‘✅ OK’)”

설치 후 확인

xmlrpc.php

워드프레스 자동 포스팅용

급식용

import requests
from bs4 import BeautifulSoup

url = “https://school.jbedu.kr/yongsan4u/M010304/”
res = requests.get(url)
soup = BeautifulSoup(res.text, “lxml”)

date = soup.find(“ul”, class_=”tch-lnc-date”).get_text(strip=True)

줄바꿈 처리된 급식 메뉴 추출

meal_items = soup.select(“dd.tch-lnc li”)
if meal_items:
data = “
“.join([item.get_text(strip=True) for item in meal_items])
else:
no_data = soup.find(“div”, class_=”tch-no-data”)
data = “급식이 없습니다.” if no_data else “정보를 불러오지 못했습니다.”

html = f”””

오늘의 급식

🍱 오늘의 급식

{date}

{data}
“””

with open(“/home/ubuntu/www/lunch.html”, “w”, encoding=”utf-8″) as f:
f.write(html)

워드프레스 업로드

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
import datetime

1. 워드프레스 로그인 정보 설정

wp_url = “https://xmlrpc.php”
wp_username = “”
wp_password = “”
client = Client(wp_url, wp_username, wp_password)

2. HTML 파일 읽기

html_path = “/var/www/html/lunch.html”
with open(html_path, “r”, encoding=”utf-8″) as f:
html_content = f.read()

3. 오늘 날짜 가져오기

today = datetime.date.today().strftime(“%Y-%m-%d”)

4. 워드프레스 글 작성

post = WordPressPost()
post.title = f”{today} 급식 정보”
post.content = html_content
post.post_status = “publish”
post.terms_names = {
‘category’: [‘급식’],
‘post_tag’: [‘점심’, ‘학교’]
}

5. 업로드 실행

client.call(NewPost(post))
print(“✅ 워드프레스에 급식 정보 게시 완료!”)

크론탭

crontab -e

30 8 * * 1-5 /var/www/html/run_lunch.sh >> /var/www/html/lunch_log.txt 2>&1

월 금 8:30 시행