import gradio as gr import requests from bs4 import BeautifulSoup import pandas as pd def scrape_jobs(url): # Fetch the webpage response = requests.get(url) response.encoding = 'utf-8' # Ensure correct encoding # Parse the HTML content soup = BeautifulSoup(response.text, 'html.parser') # Extract job titles and descriptions jobs = soup.find_all('article', class_='js-job-item') job_titles = [job.find('a', class_='js-job-link').text.strip() for job in jobs] job_descriptions = [job.find('p').text.strip() for job in jobs] # Store the extracted data in a Pandas DataFrame data = { 'Job Title': job_titles, 'Job Description': job_descriptions } df = pd.DataFrame(data) # Convert DataFrame to HTML for display in Gradio return df.to_html() # Gradio interface iface = gr.Interface( fn=scrape_jobs, inputs=gr.Textbox(label="Enter Job Search URL"), outputs="html", title="104 人力銀行職缺爬取", description="Enter a URL to scrape job titles and descriptions." ) # Launch the Gradio app iface.launch()