YU-XI commited on
Commit
f2b4a88
·
verified ·
1 Parent(s): e59d6a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py CHANGED
@@ -9,6 +9,7 @@ import json
9
  import time
10
  from pytrends.request import TrendReq
11
  import plotly.express as px
 
12
 
13
  # Set up Streamlit app title
14
  st.title("🐣MOMO 🆚 PCHOME 商品搜索和價格分析👁️‍🗨️")
@@ -17,6 +18,67 @@ st.title("🐣MOMO 🆚 PCHOME 商品搜索和價格分析👁️‍🗨️")
17
  search_keyword = st.text_input("請輸入要搜索的關鍵字: ", "筆電")
18
  page_number = st.number_input("請輸入要搜索的頁數: ", min_value=1, max_value=100, value=1, step=1)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # Create a button to start the scraping process
21
  if st.button("開始搜索"):
22
  start_time = time.time()
 
9
  import time
10
  from pytrends.request import TrendReq
11
  import plotly.express as px
12
+ from datetime import datetime, timedelta
13
 
14
  # Set up Streamlit app title
15
  st.title("🐣MOMO 🆚 PCHOME 商品搜索和價格分析👁️‍🗨️")
 
18
  search_keyword = st.text_input("請輸入要搜索的關鍵字: ", "筆電")
19
  page_number = st.number_input("請輸入要搜索的頁數: ", min_value=1, max_value=100, value=1, step=1)
20
 
21
+ # 在Pytrends Analysis部分之前添加日期選擇器
22
+ st.subheader("Google Trends 分析時間範圍")
23
+ default_end_date = datetime.now().date()
24
+ default_start_date = default_end_date - timedelta(days=7)
25
+
26
+ start_date = st.date_input("開始日期", value=default_start_date)
27
+ end_date = st.date_input("結束日期", value=default_end_date)
28
+
29
+ if start_date <= end_date:
30
+ # Pytrends Analysis
31
+ pytrend = TrendReq(hl="zh-TW", tz=-480)
32
+ keywords = search_keyword
33
+ timeframe = f"{start_date} {end_date}"
34
+ pytrend.build_payload(
35
+ kw_list=[keywords],
36
+ cat=3,
37
+ timeframe=timeframe,
38
+ geo="TW",
39
+ gprop="")
40
+
41
+ df = pytrend.interest_over_time()
42
+ if not df.empty:
43
+ if "isPartial" in df.columns:
44
+ df = df.drop(["isPartial"], axis=1)
45
+
46
+ # Plotting Trend Data
47
+ fig, ax = plt.subplots(figsize=(12, 8), dpi=80)
48
+ ax.plot(df.index, df[keywords], label=keywords, lw=3.0, marker='o', markersize=8, color='#4285F4', linestyle='-')
49
+
50
+ ax.set_title(f"Interest Over Time for {search_keyword}", fontsize=20, fontweight='bold', color='#4285F4')
51
+ ax.set_xlabel("時間", fontsize=14, fontweight='bold', color='#4285F4')
52
+ ax.set_ylabel("熱搜度", fontsize=14, fontweight='bold', color='#4285F4')
53
+ ax.legend()
54
+ ax.grid(True, linestyle='--', alpha=0.6)
55
+
56
+ # Removing spines
57
+ ax.spines['top'].set_visible(False)
58
+ ax.spines['right'].set_visible(False)
59
+
60
+ # Customize tick colors
61
+ ax.tick_params(axis='x', colors='#4285F4')
62
+ ax.tick_params(axis='y', colors='#4285F4')
63
+
64
+ # Customize legend
65
+ legend = ax.legend()
66
+ legend.get_frame().set_alpha(0.5)
67
+ legend.get_lines()[0].set_linestyle('-')
68
+
69
+ # Display the plot
70
+ plt.tight_layout()
71
+ st.pyplot(fig)
72
+
73
+ # Print out statistics
74
+ st.write(df.describe())
75
+ else:
76
+ st.write("在選定的時間範圍內沒有數據。請嘗試不同的日期範圍或關鍵字。")
77
+ else:
78
+ st.error("錯誤:結束日期必須在開始日期之後。")
79
+
80
+
81
+
82
  # Create a button to start the scraping process
83
  if st.button("開始搜索"):
84
  start_time = time.time()