Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
import gradio as gr
|
4 |
-
import random
|
5 |
import time
|
6 |
|
7 |
def convert_to_mobile_url(url):
|
8 |
"""
|
9 |
-
|
10 |
"""
|
11 |
if url.startswith("https://blog.naver.com/"):
|
12 |
url_parts = url.split("/")
|
@@ -14,61 +13,55 @@ def convert_to_mobile_url(url):
|
|
14 |
post_id = url_parts[-1]
|
15 |
mobile_url = f"https://m.blog.naver.com/{blog_id}/{post_id}"
|
16 |
return mobile_url
|
17 |
-
return url #
|
18 |
|
19 |
def scrape_naver_blog(url):
|
20 |
try:
|
21 |
-
#
|
22 |
url = convert_to_mobile_url(url)
|
23 |
|
24 |
-
# HTTP
|
25 |
headers = {
|
26 |
"User-Agent": (
|
27 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
28 |
"(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
29 |
),
|
30 |
-
"Referer": "https://www.naver.com/",
|
31 |
}
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
time.sleep(delay)
|
36 |
|
37 |
-
#
|
38 |
response = requests.get(url, headers=headers)
|
39 |
-
response.raise_for_status() #
|
40 |
|
41 |
-
#
|
42 |
soup = BeautifulSoup(response.text, 'html.parser')
|
43 |
|
44 |
-
#
|
45 |
title_div = soup.find('div', class_='se-module se-module-text se-title-text')
|
46 |
title = title_div.get_text(strip=True) if title_div else "제목을 찾을 수 없습니다."
|
47 |
|
48 |
-
#
|
49 |
text_components = soup.find_all('div', class_='se-module se-module-text')
|
50 |
content = "\n".join(component.get_text(strip=True) for component in text_components if component)
|
51 |
|
52 |
return f"제목: {title}\n내용: {content}"
|
53 |
|
54 |
except Exception as e:
|
|
|
55 |
return f"오류 발생: {e}"
|
56 |
|
57 |
-
# Gradio
|
58 |
def gradio_interface(url):
|
59 |
return scrape_naver_blog(url)
|
60 |
|
|
|
61 |
iface = gr.Interface(
|
62 |
fn=gradio_interface,
|
63 |
inputs=gr.Textbox(label="네이버 블로그 URL 입력 (표준 또는 모바일)"),
|
64 |
outputs=gr.Textbox(label="스크래핑된 블로그 내용"),
|
65 |
title="네이버 블로그 스크래퍼 (텍스트만)",
|
66 |
description=(
|
67 |
-
"네이버 블로그 URL(표준 또는 모바일
|
68 |
-
"스크립트는 표준 URL을 자동으로 모바일 형식으로 변환하며, 헤더와 랜덤 딜레이를 설정하여 요청을 자연스럽게 만듭니다."
|
69 |
-
),
|
70 |
-
theme="compact", # 간결한 Gradio 인터페이스 테마
|
71 |
-
)
|
72 |
-
|
73 |
-
if __name__ == "__main__":
|
74 |
-
iface.launch()
|
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
import gradio as gr
|
|
|
4 |
import time
|
5 |
|
6 |
def convert_to_mobile_url(url):
|
7 |
"""
|
8 |
+
표준 네이버 블로그 URL을 모바일 URL로 변환합니다.
|
9 |
"""
|
10 |
if url.startswith("https://blog.naver.com/"):
|
11 |
url_parts = url.split("/")
|
|
|
13 |
post_id = url_parts[-1]
|
14 |
mobile_url = f"https://m.blog.naver.com/{blog_id}/{post_id}"
|
15 |
return mobile_url
|
16 |
+
return url # 이미 모바일 URL이면 그대로 반환
|
17 |
|
18 |
def scrape_naver_blog(url):
|
19 |
try:
|
20 |
+
# 표준 URL을 모바일 URL로 변환
|
21 |
url = convert_to_mobile_url(url)
|
22 |
|
23 |
+
# HTTP 요청에 필요한 헤더 설정
|
24 |
headers = {
|
25 |
"User-Agent": (
|
26 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
27 |
"(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
28 |
),
|
29 |
+
"Referer": "https://www.naver.com/", # 네이버 메인 페이지를 참조 페이지로 설정
|
30 |
}
|
31 |
|
32 |
+
# 요청 전 3초 지연
|
33 |
+
time.sleep(3)
|
|
|
34 |
|
35 |
+
# URL로 GET 요청 보내기
|
36 |
response = requests.get(url, headers=headers)
|
37 |
+
response.raise_for_status() # HTTP 문제 발생 시 예외 발생
|
38 |
|
39 |
+
# HTML 내용 파싱
|
40 |
soup = BeautifulSoup(response.text, 'html.parser')
|
41 |
|
42 |
+
# 제목 추출
|
43 |
title_div = soup.find('div', class_='se-module se-module-text se-title-text')
|
44 |
title = title_div.get_text(strip=True) if title_div else "제목을 찾을 수 없습니다."
|
45 |
|
46 |
+
# 이미지 제외 텍스트 내용 추출
|
47 |
text_components = soup.find_all('div', class_='se-module se-module-text')
|
48 |
content = "\n".join(component.get_text(strip=True) for component in text_components if component)
|
49 |
|
50 |
return f"제목: {title}\n내용: {content}"
|
51 |
|
52 |
except Exception as e:
|
53 |
+
# 오류 발생 시 메시지 반환
|
54 |
return f"오류 발생: {e}"
|
55 |
|
56 |
+
# Gradio 인터페이스 함수
|
57 |
def gradio_interface(url):
|
58 |
return scrape_naver_blog(url)
|
59 |
|
60 |
+
# Gradio 인터페이스 설정
|
61 |
iface = gr.Interface(
|
62 |
fn=gradio_interface,
|
63 |
inputs=gr.Textbox(label="네이버 블로그 URL 입력 (표준 또는 모바일)"),
|
64 |
outputs=gr.Textbox(label="스크래핑된 블로그 내용"),
|
65 |
title="네이버 블로그 스크래퍼 (텍스트만)",
|
66 |
description=(
|
67 |
+
"네이버 블로그 URL(표준 또는 모바일
|
|
|
|
|
|
|
|
|
|
|
|
|
|