Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,22 +11,22 @@ from pytrends.request import TrendReq
|
|
11 |
import plotly.express as px
|
12 |
from datetime import datetime, timedelta
|
13 |
|
14 |
-
#
|
15 |
st.title("🐣MOMO 🆚 PCHOME 商品搜索和價格分析👁️🗨️")
|
16 |
|
17 |
-
#
|
18 |
search_keyword = st.text_input("請輸入要搜索的關鍵字: ", "筆電")
|
19 |
page_number = st.number_input("請輸入要搜索的頁數: ", min_value=1, max_value=100, value=1, step=1)
|
20 |
|
21 |
-
#
|
22 |
start_date = st.date_input("選擇開始日期", value=datetime.today() - timedelta(days=30))
|
23 |
end_date = st.date_input("選擇結束日期", value=datetime.today())
|
24 |
|
25 |
-
#
|
26 |
if st.button("開始搜索"):
|
27 |
start_time = time.time()
|
28 |
|
29 |
-
# MOMO
|
30 |
momo_url = "https://apisearch.momoshop.com.tw/momoSearchCloud/moec/textSearch"
|
31 |
momo_headers = {
|
32 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
|
@@ -60,13 +60,13 @@ if st.button("開始搜索"):
|
|
60 |
momo_df = pd.DataFrame(momo_product_list)
|
61 |
st.write("MOMO 商品數據:", momo_df)
|
62 |
|
63 |
-
# MOMO
|
64 |
momo_avg_price = momo_df['price'].mean()
|
65 |
st.write(f"MOMO 平均價格: {momo_avg_price:.2f}")
|
66 |
st.write(f"MOMO 最高價格: {momo_df['price'].max():.2f}")
|
67 |
st.write(f"MOMO 最低價格: {momo_df['price'].min():.2f}")
|
68 |
|
69 |
-
# MOMO
|
70 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
71 |
font_response = requests.get(font_url)
|
72 |
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
|
@@ -87,7 +87,7 @@ if st.button("開始搜索"):
|
|
87 |
plt.tight_layout()
|
88 |
st.pyplot(fig)
|
89 |
|
90 |
-
# MOMO Sunburst
|
91 |
if not momo_df.empty:
|
92 |
momo_sunburst = momo_df.groupby(['title', 'price']).size().reset_index(name='count')
|
93 |
fig_momo_sunburst = px.sunburst(
|
@@ -103,7 +103,7 @@ if st.button("開始搜索"):
|
|
103 |
|
104 |
|
105 |
|
106 |
-
# PCHOME
|
107 |
pchome_base_url = 'https://ecshweb.pchome.com.tw/search/v3.3/all/results?q='
|
108 |
pchome_data = pd.DataFrame()
|
109 |
|
@@ -119,7 +119,7 @@ if st.button("開始搜索"):
|
|
119 |
pchome_df = pchome_df[selected_columns]
|
120 |
if 'orig' in pchome_df.columns:
|
121 |
pchome_df = pchome_df.rename(columns={'orig': 'original_price'})
|
122 |
-
pchome_df['platform'] = 'PCHOME' #
|
123 |
pchome_data = pd.concat([pchome_data, pchome_df])
|
124 |
time.sleep(1)
|
125 |
else:
|
@@ -128,13 +128,13 @@ if st.button("開始搜索"):
|
|
128 |
if not pchome_data.empty:
|
129 |
st.write("PCHOME 商品數據:", pchome_data)
|
130 |
|
131 |
-
# PCHOME
|
132 |
pchome_avg_price = pchome_data['price'].mean()
|
133 |
st.write(f"PCHOME 平均價格: {pchome_avg_price:.2f}")
|
134 |
st.write(f"PCHOME 最高價格: {pchome_data['price'].max():.2f}")
|
135 |
st.write(f"PCHOME 最低價格: {pchome_data['price'].min():.2f}")
|
136 |
|
137 |
-
# PCHOME
|
138 |
fig, ax = plt.subplots(figsize=(15, 8))
|
139 |
ax.plot(pchome_data.index[:70], pchome_data['price'][:70], 'o', color='skyblue', markersize=8)
|
140 |
ax.set_title(f'PCHOME 電商網站上 "{search_keyword}" 的銷售價格', fontsize=20, fontweight='bold')
|
@@ -148,7 +148,7 @@ if st.button("開始搜索"):
|
|
148 |
plt.tight_layout()
|
149 |
st.pyplot(fig)
|
150 |
|
151 |
-
# PCHOME Sunburst
|
152 |
if not pchome_data.empty:
|
153 |
pchome_sunburst = pchome_data.groupby(['name', 'price']).size().reset_index(name='count')
|
154 |
fig_pchome_sunburst = px.sunburst(
|
@@ -160,10 +160,10 @@ if st.button("開始搜索"):
|
|
160 |
fig_pchome_sunburst.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
161 |
st.plotly_chart(fig_pchome_sunburst)
|
162 |
|
163 |
-
#
|
164 |
combined_data = pd.concat([momo_df, pchome_data], ignore_index=True)
|
165 |
|
166 |
-
#
|
167 |
csv = combined_data.to_csv(index=False, encoding='utf-8-sig').encode('utf-8-sig')
|
168 |
st.download_button(
|
169 |
label="下載CSV檔案",
|
@@ -173,9 +173,9 @@ if st.button("開始搜索"):
|
|
173 |
)
|
174 |
|
175 |
end_time = time.time()
|
176 |
-
st.write(f"
|
177 |
|
178 |
-
# Pytrends
|
179 |
pytrend = TrendReq(hl="zh-TW", tz=-480)
|
180 |
keywords = [search_keyword] # 使用列表形式
|
181 |
pytrend.build_payload(
|
@@ -190,7 +190,7 @@ if st.button("開始搜索"):
|
|
190 |
if "isPartial" in df.columns:
|
191 |
df = df.drop(["isPartial"], axis=1)
|
192 |
|
193 |
-
#
|
194 |
fig, ax = plt.subplots(figsize=(12, 8), dpi=80)
|
195 |
ax.plot(df.index, df[keywords[0]], label=keywords[0], lw=3.0, marker='o', markersize=8, color='#4285F4', linestyle='-')
|
196 |
|
@@ -200,22 +200,22 @@ if st.button("開始搜索"):
|
|
200 |
ax.legend()
|
201 |
ax.grid(True, linestyle='--', alpha=0.6)
|
202 |
|
203 |
-
#
|
204 |
ax.spines['top'].set_visible(False)
|
205 |
ax.spines['right'].set_visible(False)
|
206 |
|
207 |
-
#
|
208 |
ax.tick_params(axis='x', colors='#4285F4')
|
209 |
ax.tick_params(axis='y', colors='#4285F4')
|
210 |
|
211 |
-
#
|
212 |
legend = ax.legend()
|
213 |
legend.get_frame().set_alpha(0.5)
|
214 |
legend.get_lines()[0].set_linestyle('-')
|
215 |
|
216 |
-
#
|
217 |
plt.tight_layout()
|
218 |
st.pyplot(fig)
|
219 |
|
220 |
-
#
|
221 |
st.write(df.describe())
|
|
|
11 |
import plotly.express as px
|
12 |
from datetime import datetime, timedelta
|
13 |
|
14 |
+
# 設置 Streamlit 應用標題
|
15 |
st.title("🐣MOMO 🆚 PCHOME 商品搜索和價格分析👁️🗨️")
|
16 |
|
17 |
+
# 獲取用戶輸入
|
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 的日期範圍輸入
|
22 |
start_date = st.date_input("選擇開始日期", value=datetime.today() - timedelta(days=30))
|
23 |
end_date = st.date_input("選擇結束日期", value=datetime.today())
|
24 |
|
25 |
+
# 創建一個按鈕來開始爬取過程
|
26 |
if st.button("開始搜索"):
|
27 |
start_time = time.time()
|
28 |
|
29 |
+
# MOMO 爬取
|
30 |
momo_url = "https://apisearch.momoshop.com.tw/momoSearchCloud/moec/textSearch"
|
31 |
momo_headers = {
|
32 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
|
|
|
60 |
momo_df = pd.DataFrame(momo_product_list)
|
61 |
st.write("MOMO 商品數據:", momo_df)
|
62 |
|
63 |
+
# MOMO 數據分析
|
64 |
momo_avg_price = momo_df['price'].mean()
|
65 |
st.write(f"MOMO 平均價格: {momo_avg_price:.2f}")
|
66 |
st.write(f"MOMO 最高價格: {momo_df['price'].max():.2f}")
|
67 |
st.write(f"MOMO 最低價格: {momo_df['price'].min():.2f}")
|
68 |
|
69 |
+
# MOMO 視覺化
|
70 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
71 |
font_response = requests.get(font_url)
|
72 |
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
|
|
|
87 |
plt.tight_layout()
|
88 |
st.pyplot(fig)
|
89 |
|
90 |
+
# MOMO Sunburst圖
|
91 |
if not momo_df.empty:
|
92 |
momo_sunburst = momo_df.groupby(['title', 'price']).size().reset_index(name='count')
|
93 |
fig_momo_sunburst = px.sunburst(
|
|
|
103 |
|
104 |
|
105 |
|
106 |
+
# PCHOME 爬取
|
107 |
pchome_base_url = 'https://ecshweb.pchome.com.tw/search/v3.3/all/results?q='
|
108 |
pchome_data = pd.DataFrame()
|
109 |
|
|
|
119 |
pchome_df = pchome_df[selected_columns]
|
120 |
if 'orig' in pchome_df.columns:
|
121 |
pchome_df = pchome_df.rename(columns={'orig': 'original_price'})
|
122 |
+
pchome_df['platform'] = 'PCHOME' # 添加平台識別符
|
123 |
pchome_data = pd.concat([pchome_data, pchome_df])
|
124 |
time.sleep(1)
|
125 |
else:
|
|
|
128 |
if not pchome_data.empty:
|
129 |
st.write("PCHOME 商品數據:", pchome_data)
|
130 |
|
131 |
+
# PCHOME 數據分析
|
132 |
pchome_avg_price = pchome_data['price'].mean()
|
133 |
st.write(f"PCHOME 平均價格: {pchome_avg_price:.2f}")
|
134 |
st.write(f"PCHOME 最高價格: {pchome_data['price'].max():.2f}")
|
135 |
st.write(f"PCHOME 最低價格: {pchome_data['price'].min():.2f}")
|
136 |
|
137 |
+
# PCHOME 視覺化
|
138 |
fig, ax = plt.subplots(figsize=(15, 8))
|
139 |
ax.plot(pchome_data.index[:70], pchome_data['price'][:70], 'o', color='skyblue', markersize=8)
|
140 |
ax.set_title(f'PCHOME 電商網站上 "{search_keyword}" 的銷售價格', fontsize=20, fontweight='bold')
|
|
|
148 |
plt.tight_layout()
|
149 |
st.pyplot(fig)
|
150 |
|
151 |
+
# PCHOME Sunburst圖
|
152 |
if not pchome_data.empty:
|
153 |
pchome_sunburst = pchome_data.groupby(['name', 'price']).size().reset_index(name='count')
|
154 |
fig_pchome_sunburst = px.sunburst(
|
|
|
160 |
fig_pchome_sunburst.update_layout(margin=dict(t=50, l=25, r=25, b=25))
|
161 |
st.plotly_chart(fig_pchome_sunburst)
|
162 |
|
163 |
+
# 合併 MOMO 和 PCHOME 的數據
|
164 |
combined_data = pd.concat([momo_df, pchome_data], ignore_index=True)
|
165 |
|
166 |
+
# 添加一個按鈕以CSV格式下載合併後的數據
|
167 |
csv = combined_data.to_csv(index=False, encoding='utf-8-sig').encode('utf-8-sig')
|
168 |
st.download_button(
|
169 |
label="下載CSV檔案",
|
|
|
173 |
)
|
174 |
|
175 |
end_time = time.time()
|
176 |
+
st.write(f"執行時間: {end_time - start_time:.2f} 秒")
|
177 |
|
178 |
+
# Pytrends 分析
|
179 |
pytrend = TrendReq(hl="zh-TW", tz=-480)
|
180 |
keywords = [search_keyword] # 使用列表形式
|
181 |
pytrend.build_payload(
|
|
|
190 |
if "isPartial" in df.columns:
|
191 |
df = df.drop(["isPartial"], axis=1)
|
192 |
|
193 |
+
# 繪製趨勢數據
|
194 |
fig, ax = plt.subplots(figsize=(12, 8), dpi=80)
|
195 |
ax.plot(df.index, df[keywords[0]], label=keywords[0], lw=3.0, marker='o', markersize=8, color='#4285F4', linestyle='-')
|
196 |
|
|
|
200 |
ax.legend()
|
201 |
ax.grid(True, linestyle='--', alpha=0.6)
|
202 |
|
203 |
+
# 移除軸線
|
204 |
ax.spines['top'].set_visible(False)
|
205 |
ax.spines['right'].set_visible(False)
|
206 |
|
207 |
+
# 自訂刻度顏色
|
208 |
ax.tick_params(axis='x', colors='#4285F4')
|
209 |
ax.tick_params(axis='y', colors='#4285F4')
|
210 |
|
211 |
+
# 自訂圖例
|
212 |
legend = ax.legend()
|
213 |
legend.get_frame().set_alpha(0.5)
|
214 |
legend.get_lines()[0].set_linestyle('-')
|
215 |
|
216 |
+
# 顯示圖表
|
217 |
plt.tight_layout()
|
218 |
st.pyplot(fig)
|
219 |
|
220 |
+
# 輸出統計數據
|
221 |
st.write(df.describe())
|