wp-post-automation/archived/_archived_main_naver_blog_text.py

38 lines
1.1 KiB
Python

import requests
from bs4 import BeautifulSoup
def get_naver_blog_content(url):
# 네이버 블로그의 모바일 버전으로 리다이렉트
mobile_url = url.replace("blog.naver.com", "m.blog.naver.com")
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
# HTTP 요청
response = requests.get(mobile_url, headers=headers)
if response.status_code != 200:
print(f"Failed to fetch the page: {response.status_code}")
return None
# BeautifulSoup으로 HTML 파싱
soup = BeautifulSoup(response.text, 'html.parser')
# 본문 추출 (모바일 버전의 본문 클래스 사용)
content = soup.find("div", class_="se-main-container")
if content:
return content.get_text(strip=True)
else:
print("Failed to extract the blog content.")
return None
# 예제 URL
url = "https://blog.naver.com/kte1909/223724132196"
blog_content = get_naver_blog_content(url)
if blog_content:
print("Blog Content:")
print(blog_content)