TTL_Streamlit / app.py
YU-XI's picture
Create app.py
dddef8c verified
import streamlit as st
import requests
from bs4 import BeautifulSoup
import pandas as pd
def scrape_data(url):
try:
# Perform the HTTP request to get the HTML content
response = requests.get(url)
response.raise_for_status()
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Extract the title and price
title = soup.find("span", id="packagename").text.strip()
price = soup.find("span", id="price1", class_="price product-priceshow").text.strip()
# Create a DataFrame
data = {
"Title": [title],
"Price": [price]
}
df = pd.DataFrame(data)
return df
except Exception as ex:
st.error(f"An error occurred: {str(ex)}")
return None
def main():
st.title("Web Scraping with Streamlit")
# URL input
url = st.text_input("Enter URL")
# Scrape button
if st.button("Scrape Data"):
if url:
df = scrape_data(url)
if df is not None:
st.subheader("Scraping Results")
st.dataframe(df)
else:
st.warning("Please enter a URL")
if __name__ == "__main__":
main()