File size: 1,425 Bytes
af1157b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
from bs4 import BeautifulSoup

def scrape_product_info(url):
    try:
        # 發送請求到網頁
        response = requests.get(url)

        # 檢查請求是否成功
        if response.status_code == 200:
            # 解析網頁內容
            soup = BeautifulSoup(response.text, 'html.parser')
            
            # 抓取標題
            title_span = soup.find('span', {'id': 'packagename'})
            if title_span:
                title = title_span.get_text()
            else:
                title = '標題未找到'
            
            # 抓取價格
            price_span = soup.find('span', {'class': 'price product-priceshow'})
            if price_span:
                price = price_span.get_text()
            else:
                price = '價格未找到'
            
            return title, price
        else:
            return '請求失敗', f'狀態碼: {response.status_code}'
    except Exception as e:
        return '錯誤', str(e)

# 創建 Gradio 界面
interface = gr.Interface(
    fn=scrape_product_info,
    inputs=gr.Textbox(label="輸入產品網址", placeholder="請輸入完整的產品網址"),
    outputs=[gr.Textbox(label="標題"), gr.Textbox(label="價格")],
    title="產品信息抓取",
    description="輸入產品網址以抓取產品的標題和價格。"
)

# 啟動 Gradio 應用
interface.launch()