3.
Basic Web Scraping Example
Here’s a simple script to get you started:
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Set the target URL
url = "https://example.com/building-materials"
# Send a GET request to the website
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
# Extracting material names and prices
materials = []
prices = []
for item in soup.select('.material-item'):
name = item.select_one('.material-name').get_text(strip=True)
price = item.select_one('.material-price').get_text(strip=True)
materials.append(name)
prices.append(price)
# Create a DataFrame to organize the data
df = pd.DataFrame({
"Material": materials,
"Price": prices
})
# Save the data to an Excel file for further analysis
df.to_excel("material_prices.xlsx", index=False)
print("Scraped data saved to 'material_prices.xlsx'")