Spaces:
Sleeping
Sleeping
import requests | |
import json | |
import pandas as pd | |
import time | |
import streamlit as st | |
import plotly.express as px | |
import plotly.graph_objects as go | |
# Streamlit title and input | |
st.title("2024 0925 PCHOME 商品價格分析") | |
keyword = st.text_input("請輸入商品關鍵字", value="平板") # default keyword is "平板" | |
pages = st.number_input("要抓取的頁數", min_value=1, max_value=5, value=2, step=1) | |
# Fetch data only when button is clicked | |
if st.button("抓取數據"): | |
alldata = pd.DataFrame() | |
# Start timer to calculate execution time | |
start_time = time.time() | |
for i in range(1, pages + 1): # Grab data from specified number of pages | |
url = f'https://ecshweb.pchome.com.tw/search/v3.3/all/results?q={keyword}&page={i}&sort=sale/dc' | |
list_req = requests.get(url) | |
getdata = json.loads(list_req.content) | |
todataFrame = pd.DataFrame(getdata['prods']) # Convert to DataFrame | |
alldata = pd.concat([alldata, todataFrame], ignore_index=True) # Concatenate data | |
time.sleep(1) # Simulate delay to avoid overloading server | |
# Data Processing and Analysis | |
df = alldata[["name", "price"]] | |
# Calculate average price | |
average_price = df['price'].mean() | |
# Display the fetched data in Streamlit | |
st.subheader(f"抓取到 {len(df)} 條商品數據") | |
st.dataframe(df) | |
# Plotly Bar Chart | |
st.subheader("商品價格分佈 (柱狀圖)") | |
fig = px.bar(df[:70], x=df.index[:70], y='price', text='price', | |
title=f'PCHOME 上 "{keyword}" 商品售價', | |
labels={'price': '價格 (元)', 'index': '商品編號'}) | |
fig.update_traces(marker_color='skyblue', textposition='outside') | |
# Add a horizontal line for average price | |
fig.add_hline(y=average_price, line_dash="dash", line_color="red", | |
annotation_text=f'平均售價: {average_price:.1f}', annotation_position="bottom right") | |
# Display Bar Chart | |
st.plotly_chart(fig) | |
# Plotly Pie Chart for Price Distribution | |
st.subheader("商品價格分佈 (圓餅圖)") | |
df['price_range'] = pd.cut(df['price'], bins=[0, 5000, 10000, 15000, 20000, float('inf')], | |
labels=['0-5k', '5k-10k', '10k-15k', '15k-20k', '20k以上']) | |
pie_chart = px.pie(df, names='price_range', title='商品價格分佈') | |
st.plotly_chart(pie_chart) | |
# Plotly Sunburst Chart for Price Range and Product Name | |
st.subheader("商品價格與名稱的 Sunburst 圖") | |
sunburst_chart = px.sunburst(df[:70], path=['price_range', 'name'], values='price', | |
title='商品價格與名稱分佈 (Sunburst 圖)') | |
st.plotly_chart(sunburst_chart) | |
# Calculate and display execution time | |
end_time = time.time() | |
execution_time = end_time - start_time | |
st.write(f"執行時間:{execution_time:.2f} 秒") | |