ttl / app.py
ChiYuH's picture
Create app.py
3ecccdc verified
raw
history blame contribute delete
No virus
1.4 kB
import requests
from bs4 import BeautifulSoup
import pandas as pd
import gradio as gr
def scrape_ttl(keyword):
url = f"https://eshop.ttl.com.tw/b2b_cplistsrh.aspx?srh={keyword}"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
products = []
prices = []
# 抓取商品名稱
for product in soup.find_all('a', class_='h4'):
products.append(product.text.strip())
# 抓取商品價格
for price_box in soup.find_all('div', class_='price-box'):
special_price = price_box.find('span', class_='price-product-price')
regular_price = price_box.find('span', class_='price-product-genprice')
if special_price:
prices.append(special_price.text.strip())
elif regular_price:
prices.append(regular_price.text.strip())
else:
prices.append('N/A')
# 創建DataFrame
df = pd.DataFrame({
'商品名稱': products,
'價格': prices
})
return df
def search_and_display(keyword):
df = scrape_ttl(keyword)
return df
# 創建Gradio界面
iface = gr.Interface(
fn=search_and_display,
inputs=gr.Textbox(label="輸入關鍵字"),
outputs=gr.Dataframe(),
title="台灣菸酒商品搜索",
description="輸入關鍵字搜索台灣菸酒商品"
)
# 啟動Gradio應用
iface.launch()