Spaces:
Sleeping
Sleeping
| import time | |
| import pandas as pd | |
| import requests | |
| import json | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import matplotlib.font_manager as fm | |
| import streamlit as st | |
| from pathlib import Path | |
| # 使用者輸入 | |
| st.title("PChome 網站爬蟲分析") | |
| keyword = st.text_input("請輸入想搜尋的關鍵字", value="平板") | |
| pages = st.number_input("請輸入要抓取的頁數", min_value=1, max_value=10, value=1) | |
| # 按鈕 | |
| if st.button('開始抓取資料'): | |
| # 記錄開始時間 | |
| start_time = time.time() | |
| # 下載字型檔案 | |
| font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download" | |
| font_path = "TaipeiSansTCBeta-Regular.ttf" | |
| if not Path(font_path).is_file(): | |
| with open(font_path, "wb") as f: | |
| f.write(requests.get(font_url).content) | |
| fm.fontManager.addfont(font_path) | |
| plt.rcParams['font.family'] = 'Taipei Sans TC Beta' | |
| # 爬取PChome資料 | |
| alldata = pd.DataFrame() | |
| for i in range(1, pages + 1): | |
| 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']) | |
| alldata = pd.concat([alldata, todataFrame]) | |
| time.sleep(10) | |
| # 資料處理 | |
| df = alldata[["name", "price"]] | |
| df01 = df | |
| # 計算統計數據 | |
| mean_price = df01['price'].mean() | |
| max_name = df01.loc[df01['price'].idxmax()]['name'] | |
| min_price = df01['price'].min() | |
| st.write(f"平均價格: {mean_price}") | |
| st.write(f"最高價商品名稱: {max_name}") | |
| st.write(f"最低價格: {min_price}") | |
| # 畫圖設定 | |
| st.subheader('價格分佈圖') | |
| plt.figure(figsize=(12, 6)) | |
| sns.histplot(df01['price'], bins=30, kde=True) | |
| plt.title('商品價格分佈', fontsize=16) | |
| plt.xlabel('價格', fontsize=12) | |
| plt.ylabel('計數', fontsize=12) | |
| st.pyplot(plt) | |
| # 畫價格走勢圖 | |
| st.subheader('前70個商品價格趨勢') | |
| df01['price'][:70].plot(subplots=False, figsize=(15, 8), color='skyblue', linewidth=2, marker='o', markersize=8) | |
| plt.title('PCHOME 電商網站上平板售價', fontsize=20, fontweight='bold') | |
| plt.axhline(y=10016, color='red', linestyle='--', linewidth=2, label='價格門檻: 10016') | |
| plt.xlabel('商品編號', fontsize=14) | |
| plt.ylabel('價格', fontsize=14) | |
| plt.xticks(rotation=45, ha='right', fontsize=12) | |
| plt.yticks(fontsize=12) | |
| plt.legend(fontsize=12, loc='upper left') | |
| plt.grid(axis='y', linestyle='--', alpha=0.7) | |
| plt.tight_layout() | |
| st.pyplot(plt) | |
| # 計算執行時間 | |
| end_time = time.time() | |
| execution_time = end_time - start_time | |
| st.write(f"執行時間:{execution_time:.2f} 秒") | |