Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
import random
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# ๋๋ฒ๊น
๋ก๊ทธ ๋ ๋ฒจ ์ค์
|
8 |
+
logging.basicConfig(level=logging.DEBUG)
|
9 |
+
|
10 |
+
def scrape_blog_links(keyword):
|
11 |
+
"""
|
12 |
+
์
๋ ฅ๋ฐ์ ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ด๋ฒ ๊ฒ์ ํ์ด์ง๋ฅผ ์์ฒญํ๊ณ ,
|
13 |
+
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋งํฌ๋ง ์ถ์ถํ ํ, ๋๋คํ๊ฒ 3๊ฐ์ ๋งํฌ๋ฅผ ๋ฐํํฉ๋๋ค.
|
14 |
+
"""
|
15 |
+
base_url = "https://search.naver.com/search.naver?ssc=tab.blog.all&sm=tab_jum&query="
|
16 |
+
url = base_url + keyword
|
17 |
+
logging.debug(f"์์ฒญ URL: {url}")
|
18 |
+
|
19 |
+
try:
|
20 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
21 |
+
response = requests.get(url, headers=headers, timeout=10)
|
22 |
+
logging.debug(f"์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
23 |
+
if response.status_code != 200:
|
24 |
+
logging.error("ํ์ด์ง ์์ฒญ ์คํจ")
|
25 |
+
return "ํ์ด์ง ์์ฒญ์ ์คํจํ์์ต๋๋ค."
|
26 |
+
except Exception as e:
|
27 |
+
logging.exception("์์ฒญ ์ค ์์ธ ๋ฐ์")
|
28 |
+
return f"์์ธ ๋ฐ์: {e}"
|
29 |
+
|
30 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
31 |
+
detail_boxes = soup.find_all("div", class_="detail_box")
|
32 |
+
logging.debug(f"detail_box ๊ฐ์: {len(detail_boxes)}")
|
33 |
+
blog_links = set()
|
34 |
+
|
35 |
+
for box in detail_boxes:
|
36 |
+
a_tags = box.find_all("a")
|
37 |
+
for a in a_tags:
|
38 |
+
href = a.get("href", "")
|
39 |
+
cru = a.get("cru", "")
|
40 |
+
logging.debug(f"a ํ๊ทธ href: {href}, cru: {cru}")
|
41 |
+
# ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋งํฌ๋ง ์ถ์ถ (href ํน์ cru ์์ฑ ์ฌ์ฉ)
|
42 |
+
if href.startswith("https://blog.naver.com/"):
|
43 |
+
blog_links.add(href)
|
44 |
+
elif cru.startswith("https://blog.naver.com/"):
|
45 |
+
blog_links.add(cru)
|
46 |
+
|
47 |
+
logging.debug(f"์ถ์ถ๋ ๋ธ๋ก๊ทธ ๋งํฌ ๊ฐ์: {len(blog_links)}")
|
48 |
+
if not blog_links:
|
49 |
+
return "๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋งํฌ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
50 |
+
|
51 |
+
# ๋๋คํ๊ฒ ์ต๋ 3๊ฐ์ ๋งํฌ ์ ํ
|
52 |
+
num_links = min(3, len(blog_links))
|
53 |
+
selected_links = random.sample(blog_links, num_links)
|
54 |
+
logging.debug(f"๋๋ค์ผ๋ก ์ ํ๋ ๋งํฌ: {selected_links}")
|
55 |
+
|
56 |
+
# ๊ฐ ๋งํฌ๋ฅผ ์ค๋ฐ๊ฟ์ผ๋ก ๊ตฌ๋ถํ์ฌ ๋ฐํ
|
57 |
+
return "\n".join(selected_links)
|
58 |
+
|
59 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
60 |
+
iface = gr.Interface(
|
61 |
+
fn=scrape_blog_links,
|
62 |
+
inputs=gr.Textbox(label="ํค์๋ ์
๋ ฅ", placeholder="์: ์ค์ง์ด๊ฒ์2"),
|
63 |
+
outputs=gr.Textbox(label="๋๋ค ๋ธ๋ก๊ทธ ๋งํฌ ์ถ๋ ฅ"),
|
64 |
+
title="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋งํฌ ์ถ์ถ๊ธฐ",
|
65 |
+
description="ํค์๋๋ฅผ ์
๋ ฅํ๊ณ ์คํ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ๋ค์ด๋ฒ ๊ฒ์ ๊ฒฐ๊ณผ์์ ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋งํฌ๋ฅผ ์ถ์ถํ์ฌ ๋๋คํ๊ฒ 3๊ฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค."
|
66 |
+
)
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
logging.debug("์ฑ ์์")
|
70 |
+
iface.launch(debug=True)
|