Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
import random
|
5 |
+
import time
|
6 |
+
|
7 |
+
def scrape_naver_blog(url):
|
8 |
+
# ๊ฐ ์์ฒญ ์ฌ์ด์ 1์ด์์ 3์ด ์ฌ์ด์ ๋๋ค ๋๋ ์ด ์ ์ฉ
|
9 |
+
delay = random.uniform(1, 3)
|
10 |
+
time.sleep(delay)
|
11 |
+
|
12 |
+
# ์ฌ๋ฌ ๋ธ๋ผ์ฐ์ ์ User-Agent ๋ฌธ์์ด์ ๋๋ค์ผ๋ก ์ ํ
|
13 |
+
user_agents = [
|
14 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
|
15 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15",
|
16 |
+
"Mozilla/5.0 (Linux; Android 11; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36"
|
17 |
+
]
|
18 |
+
|
19 |
+
headers = {
|
20 |
+
"User-Agent": random.choice(user_agents),
|
21 |
+
"Referer": "https://m.blog.naver.com", # ๋ค์ด๋ฒ ๋ชจ๋ฐ์ผ ํ์ด์ง์์ ์จ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ค์
|
22 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
|
23 |
+
}
|
24 |
+
|
25 |
+
# ์ง์ ๋ URL์ GET ์์ฒญ
|
26 |
+
response = requests.get(url, headers=headers)
|
27 |
+
response.raise_for_status()
|
28 |
+
|
29 |
+
# BeautifulSoup์ผ๋ก HTML ํ์ฑ
|
30 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
31 |
+
|
32 |
+
# ์ ๋ชฉ ์ถ์ถ: <div class="se-module se-module-text se-title-text"> ๋ด๋ถ์ ํ
์คํธ ์ถ์ถ
|
33 |
+
title_div = soup.find("div", class_="se-module se-module-text se-title-text")
|
34 |
+
if title_div:
|
35 |
+
title_text = title_div.get_text(strip=True)
|
36 |
+
else:
|
37 |
+
title_text = "์ ๋ชฉ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
38 |
+
|
39 |
+
# ๋ณธ๋ฌธ ๋ด์ฉ ์ถ์ถ: <div class="se-main-container"> ๋ด๋ถ์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ถ์ถ
|
40 |
+
main_container = soup.find("div", class_="se-main-container")
|
41 |
+
if main_container:
|
42 |
+
content_text = main_container.get_text(separator="\n", strip=True)
|
43 |
+
else:
|
44 |
+
content_text = "๋ณธ๋ฌธ ๋ด์ฉ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
45 |
+
|
46 |
+
# ์ ๋ชฉ๊ณผ ๋ณธ๋ฌธ์ ํ๋์ ํ
์คํธ๋ก ๊ฒฐํฉ
|
47 |
+
result = f"์ ๋ชฉ: {title_text}\n\n๋ณธ๋ฌธ:\n{content_text}"
|
48 |
+
return result
|
49 |
+
|
50 |
+
# ๊ทธ๋ผ๋์ค ์ธํฐํ์ด์ค ๊ตฌ์ฑ (์
๋ ฅ์ ํ
์คํธ๋ฐ์ค, ์ถ๋ ฅ์ ํ
์คํธ๋ฐ์ค)
|
51 |
+
interface = gr.Interface(
|
52 |
+
fn=scrape_naver_blog,
|
53 |
+
inputs=gr.inputs.Textbox(label="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ URL ์
๋ ฅ"),
|
54 |
+
outputs=gr.outputs.Textbox(label="์คํฌ๋ํ ๊ฒฐ๊ณผ"),
|
55 |
+
title="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํฌ๋ํ ๋๊ตฌ",
|
56 |
+
description="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ์ ์ ๋ชฉ๊ณผ ๋ณธ๋ฌธ ๋ด์ฉ์ ์คํฌ๋ํํฉ๋๋ค. ๋งํฌ๋ฅผ ์
๋ ฅํ์ธ์."
|
57 |
+
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
interface.launch()
|