Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,16 +5,16 @@ import gradio as gr
|
|
5 |
def scrape_naver_blog(url):
|
6 |
try:
|
7 |
# 디버깅 로그: URL 확인
|
8 |
-
print(f"[DEBUG]
|
9 |
|
10 |
# 네이버 블로그 HTML 가져오기
|
11 |
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}
|
12 |
response = requests.get(url, headers=headers)
|
13 |
|
14 |
# HTTP 상태 코드 확인
|
15 |
-
print(f"[DEBUG] HTTP
|
16 |
if response.status_code != 200:
|
17 |
-
return f"
|
18 |
|
19 |
# HTML 파싱
|
20 |
soup = BeautifulSoup(response.text, "html.parser")
|
@@ -28,19 +28,19 @@ def scrape_naver_blog(url):
|
|
28 |
content_element = soup.select_one(content_xpath.replace(" > ", " > "))
|
29 |
|
30 |
if not title_element or not content_element:
|
31 |
-
return "
|
32 |
|
33 |
# 텍스트 추출
|
34 |
title = title_element.get_text(strip=True)
|
35 |
content = content_element.get_text(strip=True)
|
36 |
|
37 |
# 결과 반환
|
38 |
-
return f"제목: {title}
|
39 |
|
40 |
except Exception as e:
|
41 |
# 디버깅 로그: 예외 발생 시
|
42 |
-
print(f"[DEBUG]
|
43 |
-
return f"
|
44 |
|
45 |
# Gradio 인터페이스 설정
|
46 |
def gradio_interface(url):
|
@@ -48,10 +48,11 @@ def gradio_interface(url):
|
|
48 |
|
49 |
iface = gr.Interface(
|
50 |
fn=gradio_interface,
|
51 |
-
inputs=gr.Textbox(label="
|
52 |
-
outputs=gr.Textbox(label="
|
53 |
-
title="
|
54 |
-
description="
|
|
|
55 |
)
|
56 |
|
57 |
if __name__ == "__main__":
|
|
|
5 |
def scrape_naver_blog(url):
|
6 |
try:
|
7 |
# 디버깅 로그: URL 확인
|
8 |
+
print(f"[DEBUG] 스크래핑할 URL: {url}")
|
9 |
|
10 |
# 네이버 블로그 HTML 가져오기
|
11 |
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}
|
12 |
response = requests.get(url, headers=headers)
|
13 |
|
14 |
# HTTP 상태 코드 확인
|
15 |
+
print(f"[DEBUG] HTTP 응답 상태 코드: {response.status_code}")
|
16 |
if response.status_code != 200:
|
17 |
+
return f"오류: 페이지에 접근할 수 없습니다. HTTP 상태 코드: {response.status_code}"
|
18 |
|
19 |
# HTML 파싱
|
20 |
soup = BeautifulSoup(response.text, "html.parser")
|
|
|
28 |
content_element = soup.select_one(content_xpath.replace(" > ", " > "))
|
29 |
|
30 |
if not title_element or not content_element:
|
31 |
+
return "오류: 제공된 XPath로 제목이나 내용을 찾을 수 없습니다."
|
32 |
|
33 |
# 텍스트 추출
|
34 |
title = title_element.get_text(strip=True)
|
35 |
content = content_element.get_text(strip=True)
|
36 |
|
37 |
# 결과 반환
|
38 |
+
return f"제목: {title}\\n내용: {content}"
|
39 |
|
40 |
except Exception as e:
|
41 |
# 디버깅 로그: 예외 발생 시
|
42 |
+
print(f"[DEBUG] 예외 발생: {str(e)}")
|
43 |
+
return f"오류가 발생했습니다: {str(e)}"
|
44 |
|
45 |
# Gradio 인터페이스 설정
|
46 |
def gradio_interface(url):
|
|
|
48 |
|
49 |
iface = gr.Interface(
|
50 |
fn=gradio_interface,
|
51 |
+
inputs=gr.Textbox(label="네이버 블로그 URL"),
|
52 |
+
outputs=gr.Textbox(label="스크래핑 결과"),
|
53 |
+
title="네이버 블로그 스크래퍼",
|
54 |
+
description="네이버 블로그 URL을 입력하면 제목과 내용을 스크래핑하여 출력합니다.",
|
55 |
+
theme="compact",
|
56 |
)
|
57 |
|
58 |
if __name__ == "__main__":
|