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