0% found this document useful (0 votes)
2 views

3. Basic Web Scraping Example

This document provides a basic web scraping example using Python with the requests and BeautifulSoup libraries. It demonstrates how to extract material names and prices from a specified URL and save the data into an Excel file using pandas. The script includes steps for sending a GET request, parsing the HTML, and organizing the scraped data into a DataFrame.

Uploaded by

Tan Zhen Hui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3. Basic Web Scraping Example

This document provides a basic web scraping example using Python with the requests and BeautifulSoup libraries. It demonstrates how to extract material names and prices from a specified URL and save the data into an Excel file using pandas. The script includes steps for sending a GET request, parsing the HTML, and organizing the scraped data into a DataFrame.

Uploaded by

Tan Zhen Hui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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'")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy