import gradio as gr from scraper import crawl import pandas as pd def run_scraper(url, topic, depth): if not url or not topic: return "Please provide both URL and Topic.", None try: depth = int(depth) except ValueError: return "Depth must be an integer.", None results = crawl(url, topic, depth) if not results: return f"No content found for topic '{topic}' up to depth {depth}.", None # Format results for display formatted_results = [] for res in results: for match in res['matches']: formatted_results.append({ "URL": res['url'], "Matched Content": match }) df = pd.DataFrame(formatted_results) return f"Found {len(formatted_results)} matches across {len(results)} pages.", df with gr.Blocks(title="Web Content Topic Scraper") as demo: gr.Markdown("# 網站主題內容爬蟲 (Web Content Topic Scraper)") gr.Markdown("輸入目標網址與主題,爬蟲會自動尋找相關內容。") with gr.Row(): with gr.Column(): url_input = gr.Textbox(label="目標網址 (URL)", placeholder="https://www.bbc.com/news") topic_input = gr.Textbox(label="尋找主題 (Topic)", placeholder="technology") depth_input = gr.Slider(minimum=0, maximum=3, step=1, value=1, label="爬取層數 (Depth)") submit_btn = gr.Button("開始搜尋 (Search)", variant="primary") with gr.Column(): status_output = gr.Textbox(label="狀態 (Status)") results_output = gr.Dataframe(label="搜尋結果 (Results)", wrap=True) submit_btn.click( fn=run_scraper, inputs=[url_input, topic_input, depth_input], outputs=[status_output, results_output] ) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", share=False)