Step-by-Step Guide for Absolute Beginners
1. Install the Tools
First, open your computer’s Terminal (Mac/Linux) or Command Prompt (Windows). Type this command and press Enter:
pip install requests beautifulsoup4
This downloads the tools you need to scrape websites.
2. Write Your First Scraper
Create a new file named scraper.py (use Notepad, VS Code, or any text editor). Copy-paste this code:
import requests
from bs4 import BeautifulSoup
# Step 1: Get the webpage
url = "https://www.example.com" # Replace with a real website URL
response = requests.get(url)
# Step 2: Check if it worked
if response.status_code == 200:
print("Website fetched successfully!")
# Step 3: Parse the HTML
soup = BeautifulSoup(response.text, 'html.parser')
# Step 4: Extract something (e.g., page title)
title = soup.title.text
print(f"Page Title: {title}")
else:
print("Failed to fetch the website.")
3. Run the Code
In your terminal, navigate to where you saved scraper.py and run:
python scraper.py
You should see the website’s title printed. If you get errors:
- Check if you installed the libraries correctly.
- Ensure the URL works (try
https://example.com first).
4. Extract Property Information
To scrape property details (e.g., price, address), you need to:
- Find the HTML elements containing the data (right-click the website → "Inspect" to see the HTML).
- Modify the code to target those elements.
Example for a Real Estate Site (replace URL and elements):
import requests
from bs4 import BeautifulSoup
url = "https://www.realestatewebsite.com/listings"
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
# Example: Extract all property prices (adjust the HTML tag/class)
prices = soup.find_all('span', class_='price') # <span class="price">$500k</span>
for price in prices:
print(price.text)
else:
print("Error fetching the site.")
5. Next Steps
- Learn HTML basics: Know how to find elements using
class, id, or tags (e.g., <div>, <h1>).
- Avoid getting blocked: Add delays between requests (use
time.sleep(1)).
- Check the website’s rules: Look for
robots.txt (e.g., https://www.website.com/robots.txt) to see if scraping is allowed.
You’re now scraping! Start with simple projects and expand slowly. 😊