Spaces:
Running
Running
gunship999
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,76 +4,74 @@ import streamlit as st
|
|
4 |
import time
|
5 |
import random
|
6 |
|
7 |
-
#
|
8 |
url = "https://m.news.naver.com/rankingList"
|
9 |
|
10 |
-
#
|
11 |
headers = {
|
12 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
|
13 |
"Referer": "https://m.news.naver.com/"
|
14 |
}
|
15 |
|
16 |
-
#
|
17 |
def random_delay():
|
18 |
-
delay = random.uniform(1, 3) #
|
19 |
time.sleep(delay)
|
20 |
|
21 |
-
#
|
22 |
def scrape_ranking_news():
|
23 |
-
random_delay() #
|
24 |
response = requests.get(url, headers=headers)
|
25 |
soup = BeautifulSoup(response.text, "html.parser")
|
26 |
-
|
27 |
-
#
|
28 |
ranking_news_sections = soup.find_all("div", class_="rankingnews_box")
|
|
|
29 |
news_list = []
|
30 |
-
|
31 |
for section in ranking_news_sections:
|
32 |
-
|
33 |
articles = section.find_all("li")
|
34 |
|
35 |
for article in articles:
|
36 |
rank = article.find("em", class_="list_ranking_num").text
|
37 |
title = article.find("strong", class_="list_title").text
|
38 |
-
|
39 |
link = article.find("a")['href']
|
40 |
-
|
41 |
-
img_tag = article.find("img")
|
42 |
-
image = img_tag['src'] if img_tag else None
|
43 |
|
44 |
news_list.append({
|
45 |
"rank": rank,
|
46 |
"title": title,
|
47 |
-
"time":
|
48 |
"link": link,
|
49 |
"image": image,
|
50 |
-
"
|
51 |
})
|
52 |
return news_list
|
53 |
|
54 |
-
#
|
55 |
st.title("Daily News Scrap in Korea")
|
56 |
|
57 |
-
#
|
58 |
-
if st.button("
|
59 |
-
#
|
60 |
news_data = scrape_ranking_news()
|
61 |
-
|
62 |
-
#
|
63 |
num_columns = 5
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
st.image(news['image'])
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
4 |
import time
|
5 |
import random
|
6 |
|
7 |
+
# νκ² URL
|
8 |
url = "https://m.news.naver.com/rankingList"
|
9 |
|
10 |
+
# ν€λ μ€μ (User-Agent λ° Referer μ€μ )
|
11 |
headers = {
|
12 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
|
13 |
"Referer": "https://m.news.naver.com/"
|
14 |
}
|
15 |
|
16 |
+
# λλ€ λλ μ΄ μ€μ ν¨μ
|
17 |
def random_delay():
|
18 |
+
delay = random.uniform(1, 3) # 1μ΄μμ 3μ΄ μ¬μ΄μ λλ€ λλ μ΄
|
19 |
time.sleep(delay)
|
20 |
|
21 |
+
# μ€ν¬λνν λ°μ΄ν°κ° ν¬ν¨λ HTML μμ μ ν
|
22 |
def scrape_ranking_news():
|
23 |
+
random_delay() # λλ€ λλ μ΄ μ μ©
|
24 |
response = requests.get(url, headers=headers)
|
25 |
soup = BeautifulSoup(response.text, "html.parser")
|
26 |
+
|
27 |
+
# μ€ν¬λνν λ°μ΄ν°κ° ν¬ν¨λ HTML μμ μ ν
|
28 |
ranking_news_sections = soup.find_all("div", class_="rankingnews_box")
|
29 |
+
|
30 |
news_list = []
|
|
|
31 |
for section in ranking_news_sections:
|
32 |
+
office_name = section.find("strong", class_="rankingnews_name").text # μΈλ‘ μ¬λͺ
μΆμΆ
|
33 |
articles = section.find_all("li")
|
34 |
|
35 |
for article in articles:
|
36 |
rank = article.find("em", class_="list_ranking_num").text
|
37 |
title = article.find("strong", class_="list_title").text
|
38 |
+
time_posted = article.find("span", class_="list_time").text
|
39 |
link = article.find("a")['href']
|
40 |
+
image = article.find("img")['src']
|
|
|
|
|
41 |
|
42 |
news_list.append({
|
43 |
"rank": rank,
|
44 |
"title": title,
|
45 |
+
"time": time_posted,
|
46 |
"link": link,
|
47 |
"image": image,
|
48 |
+
"office": office_name
|
49 |
})
|
50 |
return news_list
|
51 |
|
52 |
+
# λμ λͺ© μΆκ°
|
53 |
st.title("Daily News Scrap in Korea")
|
54 |
|
55 |
+
# μ€ν λ²νΌ
|
56 |
+
if st.button("start"):
|
57 |
+
# λνΉ λ΄μ€ λ°μ΄ν°λ₯Ό μ€ν¬λν
|
58 |
news_data = scrape_ranking_news()
|
59 |
+
|
60 |
+
# 5x5 ννλ‘ κ°μ μΈλ‘ μ¬μ κΈ°μ¬λ₯Ό ν μ€μ λ°°μΉ
|
61 |
num_columns = 5
|
62 |
+
for news in news_data:
|
63 |
+
col_count = 0
|
64 |
+
cols = st.columns(num_columns)
|
65 |
+
|
66 |
+
for index, news in enumerate(news_data):
|
67 |
+
with cols[col_count]:
|
68 |
st.image(news['image'])
|
69 |
+
st.write(f"**{news['rank']}μ - {news['office']}**")
|
70 |
+
st.write(f"[{news['title']}]({news['link']})")
|
71 |
+
st.write(f"π {news['time']}")
|
72 |
+
col_count += 1
|
73 |
+
|
74 |
+
# 5κ° μΆλ ₯ ν μλ‘μ΄ νμΌλ‘
|
75 |
+
if col_count == num_columns:
|
76 |
+
col_count = 0
|
77 |
+
cols = st.columns(num_columns)
|