|
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') |
|
|
|
|
|
df = pd.DataFrame({ |
|
'商品名稱': products, |
|
'價格': prices |
|
}) |
|
|
|
return df |
|
|
|
def search_and_display(keyword): |
|
df = scrape_ttl(keyword) |
|
return df |
|
|
|
|
|
iface = gr.Interface( |
|
fn=search_and_display, |
|
inputs=gr.Textbox(label="輸入關鍵字"), |
|
outputs=gr.Dataframe(), |
|
title="台灣菸酒商品搜索", |
|
description="輸入關鍵字搜索台灣菸酒商品" |
|
) |
|
|
|
|
|
iface.launch() |