| import gradio as gr |
| import pandas as pd |
| import requests |
| from bs4 import BeautifulSoup |
| import time |
|
|
| def scrape_yellowpages(keyword, location, pages): |
|
|
| results = [] |
| pages = int(pages) |
|
|
| headers = { |
| "User-Agent": "Mozilla/5.0" |
| } |
|
|
| for page in range(1, pages + 1): |
| url = f"https://www.yellowpages.com/search?search_terms={keyword}&geo_location_terms={location}&page={page}" |
|
|
| response = requests.get(url, headers=headers) |
|
|
| if response.status_code != 200: |
| return None, None, "Failed to fetch data." |
|
|
| soup = BeautifulSoup(response.text, "html.parser") |
| listings = soup.find_all("div", class_="result") |
|
|
| for listing in listings: |
| try: |
| name = listing.find("a", class_="business-name").text.strip() |
| except: |
| name = "" |
|
|
| try: |
| phone = listing.find("div", class_="phones").text.strip() |
| except: |
| phone = "" |
|
|
| try: |
| address = listing.find("div", class_="street-address").text.strip() |
| except: |
| address = "" |
|
|
| results.append({ |
| "Business Name": name, |
| "Phone": phone, |
| "Address": address |
| }) |
|
|
| time.sleep(1) |
|
|
| if len(results) == 0: |
| return None, None, "No results found." |
|
|
| df = pd.DataFrame(results) |
| df.drop_duplicates(inplace=True) |
|
|
| file_name = "yellowpages_leads.csv" |
| df.to_csv(file_name, index=False) |
|
|
| return df, file_name, "Leads scraped successfully." |
|
|
|
|
| with gr.Blocks() as demo: |
|
|
| gr.Markdown("## Real Business Lead Scraper (YellowPages)") |
|
|
| keyword_input = gr.Textbox(label="Business Type (e.g. dentist)") |
| location_input = gr.Textbox(label="Location (e.g. Chicago)") |
| pages_input = gr.Number(value=1, label="Number of Pages (1 page ≈ 30 leads)") |
|
|
| scrape_button = gr.Button("Scrape Leads") |
|
|
| output_table = gr.Dataframe() |
| download_file = gr.File() |
| status_output = gr.Textbox(label="Status") |
|
|
| scrape_button.click( |
| scrape_yellowpages, |
| inputs=[keyword_input, location_input, pages_input], |
| outputs=[output_table, download_file, status_output] |
| ) |
|
|
| demo.launch() |
|
|