Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 66

Web Scraping with BeautifulSoup

In this lesson: Scrape websites with requests + BeautifulSoup — find tags, extract text and links, save to CSV — full code. In Hindi & English.

What is web scraping

Web scraping is using a program to automatically read data from web pages instead of copying by hand. In Python, requests downloads a page and BeautifulSoup reads its HTML so you can pull out exactly what you need — headings, prices, links and more.

💡 Two steps: requests fetches the raw HTML, BeautifulSoup parses it so you can search it.

requests + BeautifulSoup

Install both libraries once:

pip install requests beautifulsoup4

Then import them. requests gets the page, and BeautifulSoup (from the bs4 package) makes the HTML searchable.

Fetch and parse a page

import requests
from bs4 import BeautifulSoup

url = "https://example.com"
html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser")

print(soup.title.text)
Example Domain

requests.get(url).text downloads the HTML as a string. BeautifulSoup(...) turns it into a searchable object, and soup.title.text reads the page title.

Finding tags and text

# first matching tag
first = soup.find("h2")
print(first.text)

# a tag by its class
box = soup.find("div", class_="price")
print(box.text)

find returns the first matching tag. You can match by tag name, or narrow it with a class using class_="...". Then .text gives the text inside.

Scraping a list

headings = soup.find_all("h2")
for h in headings:
    print(h.text)
First heading
Second heading
Third heading

find_all returns every matching tag as a list. Looping over it lets you extract many items at once — perfect for lists of products, articles or links.

Scrape responsibly

  • Check the rules. Read the site's terms and its robots.txt file.
  • Do not hammer the server. Add small delays between requests.
  • Prefer an API. If the site offers an official API, use that instead.
  • Only public data. Never scrape private or login-protected content.

Common mistakes

  • Wrong tag or class name. Inspect the page's HTML first to find the exact names.
  • Calling .text on None. If find returns nothing, check before reading text.
  • Ignoring the status code. Confirm the page loaded (status 200) before parsing.
  • Scraping too fast. Rapid requests can get you blocked — be polite with delays.

Summary

  • Web scraping reads data from pages automatically.
  • Use requests to fetch and BeautifulSoup to parse.
  • find gets the first tag; find_all gets a list to loop.
  • Match by tag name or class, then read .text.
  • Scrape responsibly: check rules, add delays, prefer an API.

Next, handle the data with lists and dicts, or fetch structured data via an API.

Frequently Asked Questions

What is web scraping?
Web scraping means writing a program to automatically read data from web pages — like prices, headlines or tables — instead of copying by hand. In Python, requests fetches the page and BeautifulSoup reads the HTML.
What does BeautifulSoup do?
BeautifulSoup parses HTML into a structure you can search easily. You can find tags by name, class or id, and pull out their text or attributes — for example every <h2> heading on a page.
What is the difference between requests and BeautifulSoup?
requests downloads the raw HTML of a page. BeautifulSoup then makes sense of that HTML so you can search it. You almost always use them together: fetch, then parse.
What is the difference between find and find_all?
find returns the first matching tag, while find_all returns a list of all matching tags. Use find for one item and find_all when you want to loop over many.
Is web scraping allowed?
It depends on the site. Always check the site's terms and its robots.txt, do not overload the server with rapid requests, and prefer an official API when one exists. Scrape politely and only public data.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Course complete! Continue withData Science (60 Days) Your progress: 66 / 66 (100%) · Recommended next course Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free