Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from requests.adapters import HTTPAdapter
|
3 |
+
from requests.packages.urllib3.util.retry import Retry
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
import gradio as gr
|
6 |
+
import datetime
|
7 |
+
import pandas as pd
|
8 |
+
import xlsxwriter
|
9 |
+
|
10 |
+
def setup_session():
|
11 |
+
session = requests.Session()
|
12 |
+
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
|
13 |
+
session.mount('https://', HTTPAdapter(max_retries=retries))
|
14 |
+
return session
|
15 |
+
|
16 |
+
def get_base_url(board_select):
|
17 |
+
urls = {
|
18 |
+
"๋ง์ด๋ฒ ๋ฒ ": "https://cafe.naver.com/ArticleList.nhn?search.clubid=29434212&search.menuid=2&search.boardtype=L&userDisplay=50&search.specialmenutype=&search.totalCount=501&search.cafeId=29434212",
|
19 |
+
"๋ง์คํ๋ฆญ": "https://cafe.naver.com/ArticleList.nhn?search.clubid=10094499&search.menuid=599&search.boardtype=L&userDisplay=50&search.specialmenutype=&search.totalCount=501&search.cafeId=10094499",
|
20 |
+
"๊ด์ฃผ๋ง": "https://cafe.naver.com/ArticleList.nhn?search.clubid=26025763&search.menuid=508&search.boardtype=L&userDisplay=50&search.specialmenutype=&search.totalCount=501&search.cafeId=26025763",
|
21 |
+
"์ผํ์ง๋ฆ์ ": "https://cafe.naver.com/ArticleList.nhn?search.clubid=25729954&search.menuid=751&search.boardtype=L&userDisplay=50&search.specialmenutype=&search.totalCount=501&search.cafeId=25729954",
|
22 |
+
"๋ถ์ฐ๋ง": "https://cafe.naver.com/ArticleList.nhn?search.clubid=28707025&search.menuid=282&search.boardtype=L&userDisplay=50&search.specialmenutype=&search.totalCount=501&search.cafeId=28707025",
|
23 |
+
"์งํฌ๋ง": "https://cafe.naver.com/ArticleList.nhn?search.clubid=21442290&search.menuid=476&search.boardtype=L&userDisplay=50&search.specialmenutype=&search.totalCount=501&search.cafeId=21442290"
|
24 |
+
}
|
25 |
+
return urls.get(board_select, "Invalid board selected")
|
26 |
+
|
27 |
+
def convert_views(view_string):
|
28 |
+
if '๋ง' in view_string:
|
29 |
+
number_part = view_string.replace('๋ง', '')
|
30 |
+
return int(float(number_part) * 10000)
|
31 |
+
return int(view_string.replace(",", ""))
|
32 |
+
|
33 |
+
def extract_data_to_excel_and_html(page, board_select):
|
34 |
+
session = setup_session()
|
35 |
+
base_url = get_base_url(board_select)
|
36 |
+
if base_url == "Invalid board selected":
|
37 |
+
return "Invalid board selected", ""
|
38 |
+
|
39 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
|
40 |
+
today = datetime.datetime.now().strftime("%Y%m%d")
|
41 |
+
workbook = xlsxwriter.Workbook(f'{board_select}_{today}.xlsx')
|
42 |
+
worksheet = workbook.add_worksheet()
|
43 |
+
worksheet.write('A1', '์ ๋ชฉ')
|
44 |
+
worksheet.write('B1', '์์ฑ์ผ')
|
45 |
+
worksheet.write('C1', '์กฐํ์')
|
46 |
+
worksheet.write('D1', '์ข์์')
|
47 |
+
|
48 |
+
html_output = "<table style='width:100%; border: 1px solid black;'><tr><th>์ ๋ชฉ</th><th>์์ฑ์ผ</th><th>์กฐํ์</th><th>์ข์์</th></tr>"
|
49 |
+
row = 1
|
50 |
+
for p in range(1, page + 1):
|
51 |
+
url = f"{base_url}&search.page={p}"
|
52 |
+
response = session.get(url, headers=headers)
|
53 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
54 |
+
article_boards = soup.find_all('div', class_='article-board m-tcol-c')
|
55 |
+
if len(article_boards) < 2:
|
56 |
+
continue
|
57 |
+
article_board = article_boards[1]
|
58 |
+
rows = article_board.find_all('tr')
|
59 |
+
current_date = datetime.datetime.now().strftime("%Y.%m.%d")
|
60 |
+
|
61 |
+
for row_data in rows:
|
62 |
+
try:
|
63 |
+
a_tag = row_data.find('a', class_='article')
|
64 |
+
if a_tag:
|
65 |
+
link = a_tag['href']
|
66 |
+
title = a_tag.get_text(strip=True)
|
67 |
+
full_link = f"https://cafe.naver.com{link}"
|
68 |
+
views = convert_views(row_data.find('td', class_='td_view').get_text(strip=True))
|
69 |
+
likes = int(row_data.find('td', class_='td_likes').get_text(strip=True).replace(",", ""))
|
70 |
+
date = row_data.find('td', class_='td_date').get_text(strip=True)
|
71 |
+
if ":" in date:
|
72 |
+
date = current_date
|
73 |
+
|
74 |
+
worksheet.write_url(row, 0, full_link, string=title)
|
75 |
+
worksheet.write(row, 1, date)
|
76 |
+
worksheet.write_number(row, 2, views)
|
77 |
+
worksheet.write_number(row, 3, likes)
|
78 |
+
|
79 |
+
html_output += f"<tr><td><a href='{full_link}' target='_blank'>{title}</a></td><td>{date}</td><td>{views}</td><td>{likes}</td></tr>"
|
80 |
+
|
81 |
+
row += 1
|
82 |
+
except AttributeError:
|
83 |
+
continue
|
84 |
+
|
85 |
+
workbook.close()
|
86 |
+
html_output += "</table>"
|
87 |
+
return f"{board_select}_{today}.xlsx", html_output
|
88 |
+
|
89 |
+
with gr.Blocks() as demo:
|
90 |
+
gr.Markdown("# N์ฌ Cafe ํซ๋ ๊ฒ์ํ ํฌ๋กค๋ง")
|
91 |
+
gr.Markdown("""
|
92 |
+
ํ์ด์ง๋ฅผ ์
๋ ฅํ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.<br>
|
93 |
+
์ต๋ ํ์ด์ง์๋ 50ํ์ด์ง ์
๋๋ค.
|
94 |
+
ํ์ฉ๋ฒ ๋ฐ ์ด์ปค๋จธ์ค ๊ต์ก ๋ฌธ์๋ *** ์ผ๋ก ์ฃผ์ธ์.
|
95 |
+
""")
|
96 |
+
board_select = gr.Radio(["๋ง์ด๋ฒ ๋ฒ ", "๋ง์คํ๋ฆญ", "๊ด์ฃผ๋ง", "์ผํ์ง๋ฆ์ ", "๋ถ์ฐ๋ง", "์งํฌ๋ง"], label="๊ฒ์ํ์ ์ ํํ์ธ์")
|
97 |
+
with gr.Row():
|
98 |
+
inp = gr.Number(label="์์ง์ ์ํ์๋ ํ์ด์ง ์๋ฅผ ์
๋ ฅํ์ธ์", value=1)
|
99 |
+
btn = gr.Button("์์ง")
|
100 |
+
output_file = gr.File(label="์์
ํ์ผ๋ก ๋ค์ด๋ก๋")
|
101 |
+
output_html = gr.HTML()
|
102 |
+
btn.click(fn=extract_data_to_excel_and_html, inputs=[inp, board_select], outputs=[output_file, output_html])
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
demo.launch(share=True)
|