File size: 7,443 Bytes
6637917
 
 
 
 
a570702
6637917
 
a570702
 
6637917
a570702
e5e2741
a570702
e5e2741
 
6637917
a570702
 
6637917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a570702
6637917
 
 
 
 
 
 
 
 
 
 
 
6fd7965
a570702
6637917
a570702
 
6637917
 
 
a570702
6637917
 
 
 
 
 
a570702
6637917
 
 
 
 
 
6fd7965
6637917
 
 
a570702
 
 
c37a979
 
47addb2
6637917
 
58e3b5c
6637917
 
a570702
 
 
 
6637917
 
031b1c0
a570702
6637917
58e3b5c
6637917
 
 
 
 
 
031b1c0
2fb48b7
031b1c0
6fd7965
2fb48b7
 
 
 
a570702
 
6637917
 
031b1c0
a570702
6637917
58e3b5c
6637917
 
 
 
 
 
031b1c0
2fb48b7
031b1c0
6fd7965
2fb48b7
 
 
 
a570702
 
2fb48b7
c37a979
2fb48b7
 
 
 
 
 
 
 
 
f0b7530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd7965
 
 
f0b7530
 
6fd7965
6637917
 
a570702
6637917
 
 
 
 
 
 
 
a570702
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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)