Spaces:
Sleeping
Sleeping
| 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() | |