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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import tempfile
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
def scrap_naver_news(keyword):
|
| 11 |
+
"""
|
| 12 |
+
[๋๋ฒ๊น
๋ฉ์ธ์ง ์ถ๊ฐ]
|
| 13 |
+
1) ๊ฒ์ ํค์๋๋ก ๋ค์ด๋ฒ๋ด์ค ๊ฒ์URL ๊ตฌ์ฑ
|
| 14 |
+
2) requests๋ฅผ ํตํด HTML ๋ฐ์ดํฐ ์์
|
| 15 |
+
3) BeautifulSoup๋ก ํ์ฑํ์ฌ ๊ธฐ์ฌ ์ ๋ณด ์์ง
|
| 16 |
+
4) HTML ํ๋ก ์ ๋ฆฌ
|
| 17 |
+
5) BytesIO -> ์์ํ์ผ๋ก ์ ์ฅ -> ๋ค์ด๋ก๋ ๊ฐ๋ฅํ๋๋ก ๋ฐํ
|
| 18 |
+
"""
|
| 19 |
+
debug_msgs = []
|
| 20 |
+
|
| 21 |
+
base_url = "https://search.naver.com/search.naver?sm=tab_hty.top&where=news&ssc=tab.news.all&query="
|
| 22 |
+
target_url = base_url + keyword
|
| 23 |
+
debug_msgs.append(f"[๋๋ฒ๊ทธ] ์์ฒญ URL: {target_url}")
|
| 24 |
+
|
| 25 |
+
response = requests.get(target_url)
|
| 26 |
+
if response.status_code != 200:
|
| 27 |
+
debug_msgs.append(f"[์ค๋ฅ] ๋ค์ด๋ฒ ๋ด์ค ๊ฒ์ ์คํจ (์๋ต ์ฝ๋: {response.status_code})")
|
| 28 |
+
return "์ค๋ฅ ๋ฐ์", None, debug_msgs
|
| 29 |
+
|
| 30 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 31 |
+
|
| 32 |
+
# div.news_area ๋ด๋ถ์ ๊ธฐ์ฌ ์ ๋ณด๊ฐ ์กด์ฌ(์์ฒญ ์์ ๊ตฌ์กฐ ์ฐธ๊ณ )
|
| 33 |
+
news_list = soup.select("div.news_area")
|
| 34 |
+
debug_msgs.append(f"[๋๋ฒ๊ทธ] news_area ์ถ์ถ ๊ฐ์: {len(news_list)}")
|
| 35 |
+
|
| 36 |
+
results = []
|
| 37 |
+
|
| 38 |
+
for idx, news in enumerate(news_list):
|
| 39 |
+
# ์ ๋ฌธ์ฌ
|
| 40 |
+
try:
|
| 41 |
+
press = news.select_one(".info.press").get_text(strip=True)
|
| 42 |
+
except:
|
| 43 |
+
press = "ํ์ธ๋ถ๊ฐ"
|
| 44 |
+
|
| 45 |
+
# ๋ ์ง/๋ฐํ์ผ
|
| 46 |
+
# - .info_group > .info:nth-last-child(1) ํํ๋ก ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์
|
| 47 |
+
# - ์ผ์ ์ ๋ณด๊ฐ ์ฌ๋ฌ ๊ฐ๋ฉด ๋ง์ง๋ง ๊ฒ(๋๋ ์ค๊ฐ) ๋ฑ์ ๊ณ ๋ ค
|
| 48 |
+
try:
|
| 49 |
+
info_group = news.select_one(".info_group")
|
| 50 |
+
# info ํ๊ทธ๊ฐ ์ฌ๋ฌ๊ฐ ์์ ์ ์์ผ๋ '1์ผ ์ ', '3์๊ฐ ์ ' ๋ฑ๋ง ๊ณจ๋ผ๋ผ ์๋ ์์
|
| 51 |
+
# ์ฌ๊ธฐ์๋ ๊ฐ์ฅ ๋ง์ง๋ง .info๋ฅผ ์ถ์ถ(๋ ์ง ๋๋ '๋ค์ด๋ฒ๋ด์ค' ๋ฑ์ผ ์ ์์)
|
| 52 |
+
# ์ค์ ๋ก๋ ์ธ๋ก ์ฌ ์์ ์๋๊ฒ ๋ ์ง์ด๋ฏ๋ก, ์ํฉ์ ๋ฐ๋ผ ํ์ฑ์ด ๋ฌ๋ผ์ง ์ ์์
|
| 53 |
+
info_all = info_group.select(".info")
|
| 54 |
+
date = info_all[-1].get_text(strip=True) if info_all else "ํ์ธ๋ถ๊ฐ"
|
| 55 |
+
# '๋ค์ด๋ฒ๋ด์ค' ๋ฑ ํ
์คํธ๊ฐ ๋ค์ด์์ ์ ์์ผ๋ฏ๋ก ๋ ์ง ํํ๊ฐ ์๋๋ฉด ์ฌ์ฒ๋ฆฌ ๊ฐ๋ฅ
|
| 56 |
+
# ์ฌ๊ธฐ์๋ ๋จ์ ์์๋ก ๋์ด๊ฐ
|
| 57 |
+
except:
|
| 58 |
+
date = "ํ์ธ๋ถ๊ฐ"
|
| 59 |
+
|
| 60 |
+
# ์ ๋ชฉ & ๋งํฌ
|
| 61 |
+
try:
|
| 62 |
+
title_elem = news.select_one(".news_tit")
|
| 63 |
+
title = title_elem.get("title", "").strip()
|
| 64 |
+
link = title_elem.get("href", "").strip()
|
| 65 |
+
except:
|
| 66 |
+
title = "์ ๋ชฉํ์ธ๋ถ๊ฐ"
|
| 67 |
+
link = ""
|
| 68 |
+
|
| 69 |
+
# ๋ด์ค ๊ฐ๋ต์ ๋ณด
|
| 70 |
+
try:
|
| 71 |
+
desc_elem = news.select_one(".news_dsc .api_txt_lines")
|
| 72 |
+
desc = desc_elem.get_text(strip=True) if desc_elem else "๋ด์ฉํ์ธ๋ถ๊ฐ"
|
| 73 |
+
except:
|
| 74 |
+
desc = "๋ด์ฉํ์ธ๋ถ๊ฐ"
|
| 75 |
+
|
| 76 |
+
debug_msgs.append(f"[๋๋ฒ๊ทธ] {idx+1}๋ฒ์งธ ๊ธฐ์ฌ ํ์ฑ๊ฒฐ๊ณผ -> ์ ๋ฌธ์ฌ: {press}, ๋ฐํ์ผ: {date}, ์ ๋ชฉ: {title}, ๋งํฌ: {link}")
|
| 77 |
+
|
| 78 |
+
results.append({
|
| 79 |
+
"์ ๋ฌธ์ฌ": press,
|
| 80 |
+
"๋ฐํ์ผ": date,
|
| 81 |
+
"์ ๋ชฉ": title,
|
| 82 |
+
"๋ด์ค๊ฐ๋ต์ ๋ณด": desc,
|
| 83 |
+
"๋งํฌ": link
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
# ------------------------------
|
| 87 |
+
# HTML ํ
์ด๋ธ ์์ฑ
|
| 88 |
+
# ------------------------------
|
| 89 |
+
table_html = """
|
| 90 |
+
<table border="1" style="border-collapse: collapse; width: 100%;">
|
| 91 |
+
<thead>
|
| 92 |
+
<tr>
|
| 93 |
+
<th style="padding: 5px;">์ ๋ฌธ์ฌ</th>
|
| 94 |
+
<th style="padding: 5px;">๋ฐํ์ผ</th>
|
| 95 |
+
<th style="padding: 5px;">์ ๋ชฉ</th>
|
| 96 |
+
<th style="padding: 5px;">๋ด์ค๊ฐ๋ต์ ๋ณด</th>
|
| 97 |
+
<th style="padding: 5px;">๋งํฌ</th>
|
| 98 |
+
</tr>
|
| 99 |
+
</thead>
|
| 100 |
+
<tbody>
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
for row in results:
|
| 104 |
+
table_html += "<tr>"
|
| 105 |
+
table_html += f"<td style='padding: 5px;'>{row['์ ๋ฌธ์ฌ']}</td>"
|
| 106 |
+
table_html += f"<td style='padding: 5px;'>{row['๋ฐํ์ผ']}</td>"
|
| 107 |
+
table_html += f"<td style='padding: 5px;'>{row['์ ๋ชฉ']}</td>"
|
| 108 |
+
table_html += f"<td style='padding: 5px;'>{row['๋ด์ค๊ฐ๋ต์ ๋ณด']}</td>"
|
| 109 |
+
# ๋งํฌ๋ ํด๋ฆญ ๊ฐ๋ฅํ๋๋ก a ํ๊ทธ ์ฝ์
|
| 110 |
+
table_html += f"<td style='padding: 5px;'><a href='{row['๋งํฌ']}' target='_blank'>๋ฐ๋ก๊ฐ๊ธฐ</a></td>"
|
| 111 |
+
table_html += "</tr>"
|
| 112 |
+
|
| 113 |
+
table_html += """
|
| 114 |
+
</tbody>
|
| 115 |
+
</table>
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
# ------------------------------
|
| 119 |
+
# ์์
(Excel) ์์ฑ
|
| 120 |
+
# ------------------------------
|
| 121 |
+
df = pd.DataFrame(results)
|
| 122 |
+
output_io = BytesIO()
|
| 123 |
+
with pd.ExcelWriter(output_io, engine="openpyxl") as writer:
|
| 124 |
+
df.to_excel(writer, index=False, sheet_name="๋ค์ด๋ฒ๋ด์ค")
|
| 125 |
+
output_io.seek(0) # ํฌ์ธํฐ ์์น ์ด๊ธฐํ
|
| 126 |
+
|
| 127 |
+
# ์์ํ์ผ์ ์ฐ๊ธฐ
|
| 128 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 129 |
+
tmp.write(output_io.read())
|
| 130 |
+
tmp_path = tmp.name
|
| 131 |
+
|
| 132 |
+
debug_msgs.append(f"[๋๋ฒ๊ทธ] ์์
์์ํ์ผ ์์ฑ ์๋ฃ -> {tmp_path}")
|
| 133 |
+
|
| 134 |
+
return table_html, tmp_path, debug_msgs
|
| 135 |
+
|
| 136 |
+
def run_search(keyword):
|
| 137 |
+
"""
|
| 138 |
+
Gradio ์ธํฐํ์ด์ค์์ ํธ์ถ๋๋ ํจ์
|
| 139 |
+
- HTML ์ถ๋ ฅ
|
| 140 |
+
- ํ์ผ ๋ค์ด๋ก๋
|
| 141 |
+
- ๋๋ฒ๊ทธ ๋ฉ์ธ์ง
|
| 142 |
+
"""
|
| 143 |
+
table_html, file_path, debug_info = scrap_naver_news(keyword)
|
| 144 |
+
|
| 145 |
+
if file_path is None:
|
| 146 |
+
return table_html, None, "\n".join(debug_info)
|
| 147 |
+
|
| 148 |
+
# Gradio์์ ํ์ผ์ ์
๋ฐ์ดํธํ ๋๋ ๋ฐํ๊ฐ์ผ๋ก ํ์ผ ๊ฒฝ๋ก ์ง์
|
| 149 |
+
return table_html, file_path, "\n".join(debug_info)
|
| 150 |
+
|
| 151 |
+
def launch_app():
|
| 152 |
+
with gr.Blocks() as demo:
|
| 153 |
+
gr.Markdown("## ๋ค์ด๋ฒ ๋ด์ค ์คํฌ๋ํ ์์")
|
| 154 |
+
|
| 155 |
+
with gr.Row():
|
| 156 |
+
keyword_input = gr.Textbox(label="๊ฒ์ ํค์๋ ์
๋ ฅ", placeholder="์: ์ค์ง์ด")
|
| 157 |
+
|
| 158 |
+
with gr.Row():
|
| 159 |
+
search_button = gr.Button("์คํฌ๋ํ ์์")
|
| 160 |
+
|
| 161 |
+
# ๊ฒฐ๊ณผ ์ถ๋ ฅ (HTML)
|
| 162 |
+
result_html = gr.HTML(label="์คํฌ๋ํ ๊ฒฐ๊ณผ (ํ ํ์)")
|
| 163 |
+
|
| 164 |
+
# ์์
๋ค์ด๋ก๋
|
| 165 |
+
download_file = gr.File(label="์์
๋ค์ด๋ก๋")
|
| 166 |
+
|
| 167 |
+
# ๋๋ฒ๊ทธ ๋ฉ์ธ์ง
|
| 168 |
+
debug_box = gr.Textbox(label="๋๋ฒ๊ทธ ๋ก๊ทธ", lines=10)
|
| 169 |
+
|
| 170 |
+
# ๋ฒํผ ๋์ ์ ์
|
| 171 |
+
search_button.click(
|
| 172 |
+
fn=run_search,
|
| 173 |
+
inputs=[keyword_input],
|
| 174 |
+
outputs=[result_html, download_file, debug_box]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
demo.launch()
|
| 178 |
+
|
| 179 |
+
if __name__ == "__main__":
|
| 180 |
+
launch_app()
|