Spaces:
Sleeping
Sleeping
File size: 1,895 Bytes
253c112 b17de7d 253c112 b17de7d 253c112 b17de7d 253c112 b17de7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import streamlit as st
import pandas as pd
import requests
import json
import matplotlib.pyplot as plt
import seaborn as sns
st.set_page_config(page_title="PChome 行李箱價格分析", layout="wide")
st.title("🧳 PChome 行李箱爬蟲與價格分析")
keyword = st.text_input("請輸入要搜尋的商品關鍵字", "行李箱")
if st.button("開始爬蟲"):
with st.spinner("正在抓取資料中,請稍候..."):
alldata = pd.DataFrame()
for i in range(1, 2): # 可調整頁數
url = f'https://ecshweb.pchome.com.tw/search/v3.3/all/results?q={keyword}&page={i}&sort=sale/dc'
r = requests.get(url)
getdata = json.loads(r.content)
df = pd.DataFrame(getdata['prods'])
alldata = pd.concat([alldata, df])
df = alldata[["name", "price"]]
st.success("✅ 抓取完成!")
st.subheader("📄 前幾筆資料")
st.dataframe(df.head(10))
st.subheader("📊 價格統計")
st.write("平均價格:", df['price'].mean())
st.write("最高價格:", df['price'].max())
st.write("最低價格:", df['price'].min())
st.subheader("📈 價格前 70 筆折線圖")
fig, ax = plt.subplots(figsize=(12, 6))
df['price'][:70].plot(ax=ax, marker='o', color='skyblue')
ax.axhline(y=df['price'].mean(), color='red', linestyle='--', label='平均價')
ax.set_title(f"{keyword} 前 70 筆價格圖")
ax.set_xlabel("商品序號")
ax.set_ylabel("價格")
ax.legend()
st.pyplot(fig)
st.subheader("📉 價格分布直方圖")
fig2, ax2 = plt.subplots(figsize=(12, 6))
sns.histplot(df['price'], bins=30, kde=True, ax=ax2)
ax2.set_title("價格分佈圖")
ax2.set_xlabel("價格")
ax2.set_ylabel("商品數量")
st.pyplot(fig2)
|