Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
def scrape_data(url):
|
| 7 |
+
try:
|
| 8 |
+
# Perform the HTTP request to get the HTML content
|
| 9 |
+
response = requests.get(url)
|
| 10 |
+
response.raise_for_status()
|
| 11 |
+
|
| 12 |
+
# Parse the HTML content using BeautifulSoup
|
| 13 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 14 |
+
|
| 15 |
+
# Extract the title and price
|
| 16 |
+
title = soup.find("span", id="packagename").text.strip()
|
| 17 |
+
price = soup.find("span", id="price1", class_="price product-priceshow").text.strip()
|
| 18 |
+
|
| 19 |
+
# Create a DataFrame
|
| 20 |
+
data = {
|
| 21 |
+
"Title": [title],
|
| 22 |
+
"Price": [price]
|
| 23 |
+
}
|
| 24 |
+
df = pd.DataFrame(data)
|
| 25 |
+
|
| 26 |
+
return df
|
| 27 |
+
|
| 28 |
+
except Exception as ex:
|
| 29 |
+
st.error(f"An error occurred: {str(ex)}")
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
st.title("Web Scraping with Streamlit")
|
| 34 |
+
|
| 35 |
+
# URL input
|
| 36 |
+
url = st.text_input("Enter URL")
|
| 37 |
+
|
| 38 |
+
# Scrape button
|
| 39 |
+
if st.button("Scrape Data"):
|
| 40 |
+
if url:
|
| 41 |
+
df = scrape_data(url)
|
| 42 |
+
if df is not None:
|
| 43 |
+
st.subheader("Scraping Results")
|
| 44 |
+
st.dataframe(df)
|
| 45 |
+
else:
|
| 46 |
+
st.warning("Please enter a URL")
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
main()
|