import requests import pandas as pd import json import time import plotly.graph_objs as go import streamlit as st from pytrends.request import TrendReq # Streamlit app title st.title("MOMO & PCHOME 商品搜索和價格分析 + Google 趨勢") # Input fields search_keyword = st.text_input("請輸入要搜索的關鍵字:", "平板") page_number = st.number_input("請輸入要搜索的頁數:", value=1, min_value=1, max_value=100) start_date = st.text_input("請輸入 Google 趨勢的開始日期 (格式: YYYY-MM-DD):", "2024-08-01") end_date = st.text_input("請輸入 Google 趨勢的結束日期 (格式: YYYY-MM-DD):", "2024-08-11") # Create a button to start the process if st.button("開始搜索"): # MOMO scraping momo_url = "https://apisearch.momoshop.com.tw/momoSearchCloud/moec/textSearch" momo_headers = { "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" } momo_payload = { "host": "momoshop", "flag": "searchEngine", "data": { "searchValue": search_keyword, "curPage": str(page_number), "priceS": "0", "priceE": "9999999", "searchType": "1" } } momo_response = requests.post(momo_url, headers=momo_headers, json=momo_payload) momo_df = pd.DataFrame() if momo_response.status_code == 200: momo_data = momo_response.json().get('rtnSearchData', {}).get('goodsInfoList', []) momo_product_list = [] for product in momo_data: name = product.get('goodsName', '') price = product.get('goodsPrice', '') price_str = str(price).split('(')[0].replace(',', '').replace('$', '') try: product_price = float(price_str) except ValueError: product_price = 0 momo_product_list.append({'title': name, 'price': product_price, 'source': 'MOMO'}) momo_df = pd.DataFrame(momo_product_list) st.write("MOMO 商品數據:", momo_df) # PCHOME scraping pchome_base_url = 'https://ecshweb.pchome.com.tw/search/v3.3/all/results?q=' pchome_data = pd.DataFrame() for i in range(1, page_number + 1): pchome_url = f'{pchome_base_url}{search_keyword}&page={i}&sort=sale/dc' pchome_response = requests.get(pchome_url) if pchome_response.status_code == 200: pchome_json_data = json.loads(pchome_response.content) pchome_df = pd.DataFrame(pchome_json_data['prods']) # Safely select only available columns available_columns = ['name', 'describe', 'price', 'orig'] selected_columns = [col for col in available_columns if col in pchome_df.columns] pchome_df = pchome_df[selected_columns] if 'orig' in pchome_df.columns: pchome_df = pchome_df.rename(columns={'orig': 'original_price'}) pchome_df['source'] = 'PCHOME' pchome_data = pd.concat([pchome_data, pchome_df]) time.sleep(1) if not pchome_data.empty: st.write("PCHOME 商品數據:", pchome_data) # Combine MOMO and PCHOME data for overall analysis combined_df = pd.concat([momo_df, pchome_data], ignore_index=True) # Google Trends Analysis pytrend = TrendReq(hl="zh-TW", tz=-480) time_range = f'{start_date} {end_date}' pytrend.build_payload(kw_list=[search_keyword], cat=0, timeframe=time_range, geo="TW", gprop="") trend_data = pytrend.interest_over_time().drop(columns=["isPartial"]) if not trend_data.empty: st.write("Google 趨勢數據:", trend_data) # MOMO Plot if not momo_df.empty: momo_avg_price = momo_df['price'].mean() momo_fig = go.Figure() momo_fig.add_trace(go.Scatter( x=momo_df['title'], y=momo_df['price'], mode='markers', marker=dict(color='blue'), name='MOMO 價格' )) momo_fig.add_hline(y=momo_avg_price, line_dash="dash", line_color="red", annotation_text=f'平均價格: {momo_avg_price:.2f}', annotation_position="top right") momo_fig.update_layout( title=f'MOMO 電商網站上 "{search_keyword}" 的銷售價格 (平均價格: {momo_avg_price:.2f})', xaxis_title='商品名稱', yaxis_title='價格', yaxis=dict(range=[0, momo_df['price'].max() * 1.2]), # Extend Y-axis xaxis_tickfont=dict(size=10, family="Arial, italic") # Italic and smaller font for product names ) st.plotly_chart(momo_fig) # PCHOME Plot if not pchome_data.empty: pchome_avg_price = pchome_data['price'].mean() pchome_fig = go.Figure() pchome_fig.add_trace(go.Scatter( x=pchome_data['name'], y=pchome_data['price'], mode='markers', marker=dict(color='green'), name='PCHOME 價格' )) pchome_fig.add_hline(y=pchome_avg_price, line_dash="dash", line_color="red", annotation_text=f'平均價格: {pchome_avg_price:.2f}', annotation_position="top right") pchome_fig.update_layout( title=f'PCHOME 電商網站上 "{search_keyword}" 的銷售價格 (平均價格: {pchome_avg_price:.2f})', xaxis_title='商品名稱', yaxis_title='價格', yaxis=dict(range=[0, pchome_data['price'].max() * 1.2]), # Extend Y-axis xaxis_tickfont=dict(size=10, family="Arial, italic") # Italic and smaller font for product names ) st.plotly_chart(pchome_fig) # Pie Chart based on prices if not combined_df.empty: pie_fig = go.Figure(go.Pie( labels=combined_df['title'], values=combined_df['price'], textinfo='label+percent', insidetextorientation='radial' )) pie_fig.update_layout(title="商品價格比例圖") st.plotly_chart(pie_fig) # MOMO Sunburst Chart if not momo_df.empty: sunburst_momo_fig = go.Figure(go.Sunburst( labels=momo_df['title'], parents=momo_df['source'], values=momo_df['price'], branchvalues='total', textinfo='label+percent parent' )) sunburst_momo_fig.update_layout(title="MOMO 商品價格 Sunburst 圖") st.plotly_chart(sunburst_momo_fig) # PCHOME Sunburst Chart if not pchome_data.empty: sunburst_pchome_fig = go.Figure(go.Sunburst( labels=pchome_data['name'], parents=pchome_data['source'], values=pchome_data['price'], branchvalues='total', textinfo='label+percent parent' )) sunburst_pchome_fig.update_layout(title="PCHOME 商品價格 Sunburst 圖") st.plotly_chart(sunburst_pchome_fig) # Google Trends Plot if not trend_data.empty: trends_fig = go.Figure() trends_fig.add_trace(go.Scatter( x=trend_data.index, y=trend_data[search_keyword], mode='lines', line=dict(color='purple'), name='Google 趨勢' )) trends_fig.update_layout(title=f'Google 趨勢 - "{search_keyword}"', xaxis_title='時間', yaxis_title='熱門度') st.plotly_chart(trends_fig)