Spaces:
Build error
Build error
File size: 1,259 Bytes
dddef8c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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()
|