YU-XI commited on
Commit
36f7670
·
verified ·
1 Parent(s): 89dc76a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -30
app.py CHANGED
@@ -18,6 +18,14 @@ 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
  # Create a button to start the scraping process
22
  if st.button("開始搜索"):
23
  start_time = time.time()
@@ -171,46 +179,53 @@ if st.button("開始搜索"):
171
  end_time = time.time()
172
  st.write(f"Execution time: {end_time - start_time:.2f} seconds")
173
 
 
174
  # Pytrends Analysis
175
  pytrend = TrendReq(hl="zh-TW", tz=-480)
176
- keywords = search_keyword #20240204換關鍵字
 
177
  pytrend.build_payload(
178
- kw_list=keywords,
179
  cat=3,
180
- timeframe="2024-06-18 2024-06-24",
181
  geo="TW",
182
  gprop="")
183
 
184
  df = pytrend.interest_over_time()
185
- if "isPartial" in df.columns:
186
- df = df.drop(["isPartial"], axis=1)
187
-
188
- # Plotting Trend Data
189
- fig, ax = plt.subplots(figsize=(12, 8), dpi=80)
190
- ax.plot(df.index, df[keywords[0]], label=keywords[0], lw=3.0, marker='o', markersize=8, color='#4285F4', linestyle='-')
191
-
192
- ax.set_title("Interest Over Time for "+search_keyword, fontsize=20, fontweight='bold', color='#4285F4')
193
- ax.set_xlabel("時間", fontsize=14, fontweight='bold', color='#4285F4')
194
- ax.set_ylabel("熱搜度", fontsize=14, fontweight='bold', color='#4285F4')
195
- ax.legend()
196
- ax.grid(True, linestyle='--', alpha=0.6)
197
 
198
- # Removing spines
199
- ax.spines['top'].set_visible(False)
200
- ax.spines['right'].set_visible(False)
 
 
 
 
 
 
201
 
202
- # Customize tick colors
203
- ax.tick_params(axis='x', colors='#4285F4')
204
- ax.tick_params(axis='y', colors='#4285F4')
205
 
206
- # Customize legend
207
- legend = ax.legend()
208
- legend.get_frame().set_alpha(0.5)
209
- legend.get_lines()[0].set_linestyle('-')
210
 
211
- # Display the plot
212
- plt.tight_layout()
213
- st.pyplot(fig)
 
214
 
215
- # Print out statistics
216
- st.write(df.describe())
 
 
 
 
 
 
 
 
 
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
  # Create a button to start the scraping process
30
  if st.button("開始搜索"):
31
  start_time = time.time()
 
179
  end_time = time.time()
180
  st.write(f"Execution time: {end_time - start_time:.2f} seconds")
181
 
182
+ if start_date <= end_date:
183
  # Pytrends Analysis
184
  pytrend = TrendReq(hl="zh-TW", tz=-480)
185
+ keywords = search_keyword
186
+ timeframe = f"{start_date} {end_date}"
187
  pytrend.build_payload(
188
+ kw_list=[keywords],
189
  cat=3,
190
+ timeframe=timeframe,
191
  geo="TW",
192
  gprop="")
193
 
194
  df = pytrend.interest_over_time()
195
+ if not df.empty:
196
+ if "isPartial" in df.columns:
197
+ df = df.drop(["isPartial"], axis=1)
 
 
 
 
 
 
 
 
 
198
 
199
+ # Plotting Trend Data
200
+ fig, ax = plt.subplots(figsize=(12, 8), dpi=80)
201
+ ax.plot(df.index, df[keywords], label=keywords, lw=3.0, marker='o', markersize=8, color='#4285F4', linestyle='-')
202
+
203
+ ax.set_title(f"Interest Over Time for {search_keyword}", fontsize=20, fontweight='bold', color='#4285F4')
204
+ ax.set_xlabel("時間", fontsize=14, fontweight='bold', color='#4285F4')
205
+ ax.set_ylabel("熱搜度", fontsize=14, fontweight='bold', color='#4285F4')
206
+ ax.legend()
207
+ ax.grid(True, linestyle='--', alpha=0.6)
208
 
209
+ # Removing spines
210
+ ax.spines['top'].set_visible(False)
211
+ ax.spines['right'].set_visible(False)
212
 
213
+ # Customize tick colors
214
+ ax.tick_params(axis='x', colors='#4285F4')
215
+ ax.tick_params(axis='y', colors='#4285F4')
 
216
 
217
+ # Customize legend
218
+ legend = ax.legend()
219
+ legend.get_frame().set_alpha(0.5)
220
+ legend.get_lines()[0].set_linestyle('-')
221
 
222
+ # Display the plot
223
+ plt.tight_layout()
224
+ st.pyplot(fig)
225
+
226
+ # Print out statistics
227
+ st.write(df.describe())
228
+ else:
229
+ st.write("在選定的時間範圍內沒有數據。請嘗試不同的日期範圍或關鍵字。")
230
+ else:
231
+ st.error("錯誤:結束日期必須在開始日期之後。")