Spaces:
Sleeping
Sleeping
import streamlit as st | |
import matplotlib.pyplot as plt | |
import matplotlib as mpl | |
import matplotlib.font_manager as fm | |
from pytrends.request import TrendReq | |
import requests | |
# 字型設定 | |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download" | |
font_response = requests.get(font_url) | |
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file: | |
font_file.write(font_response.content) | |
fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf") | |
mpl.rc('font', family='Taipei Sans TC Beta') | |
# Streamlit 網頁設置 | |
st.set_page_config(page_title="網路熱搜度分析", layout="wide") | |
st.title("網路熱搜度分析系統") | |
# 輸入欄位 | |
keywords = st.text_input("請輸入關鍵字(多個關鍵字用逗號隔開)", "寶林, 食安, 廚師, 漢來").split(',') | |
start_analysis = st.button("分析") | |
if start_analysis: | |
# 網路熱搜度分析 | |
pytrend = TrendReq(hl='en-US', tz=360) | |
pytrend.build_payload(keywords, cat=0, timeframe='today 1-m', geo='', gprop='') | |
df_trends = pytrend.interest_over_time().drop(["isPartial"], axis=1) | |
st.subheader("網路熱搜度分析") | |
fig, ax = plt.subplots(figsize=(12, 8)) | |
colors = ["blue", "orange", "red", "green", "purple"] | |
for i, keyword in enumerate(keywords): | |
plt.plot(df_trends.index, df_trends[keyword], label=keyword, color=colors[i], lw=3.0) | |
ax.set_title("各家食安的網路熱搜度", fontsize=20, fontweight='bold', color='#4285F4') | |
ax.set_xlabel("時間", fontsize=14, fontweight='bold', color='#4285F4') | |
ax.set_ylabel("熱搜度", fontsize=14, fontweight='bold', color='#4285F4') | |
ax.legend() | |
ax.grid(True, linestyle='--', alpha=0.6) | |
ax.spines['top'].set_visible(False) | |
ax.spines['right'].set_visible(False) | |
ax.tick_params(axis='x', colors='#4285F4') | |
ax.tick_params(axis='y', colors='#4285F4') | |
st.pyplot(fig) | |