📘 টিউটোরিয়াল ১: Selenium দিয়ে একটি পেজ স্ক্র্যাপ করা
লক্ষ্য: https://books.toscrape.com/ সাইটের প্রথম পেজ থেকে বইয়ের নাম ও দাম সংগ্রহ
# ✅ প্রয়োজনীয় লাইব্রেরি ইমপোর্ট
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import time
# ✅ ব্রাউজার চালু করা
driver = webdriver.Chrome()
# ✅ টার্গেট ওয়েবসাইট ওপেন
driver.get("https://books.toscrape.com/")
time.sleep(2)
# ✅ সব বই খুঁজে বের করা
books = driver.find_elements(By.CLASS_NAME, "product_pod")
# ✅ প্রতিটি বই থেকে নাম ও দাম প্রিন্ট
for book in books:
title = book.find_element(By.TAG_NAME, 'h3').text
price = book.find_element(By.CLASS_NAME, 'price_color').text
print(f"{title} - {price}")
# ✅ ব্রাউজার বন্ধ
driver.quit()
📙 টিউটোরিয়াল ২: একাধিক পেজ স্ক্র্যাপ করা (Pagination সহ)
লক্ষ্য: সব পেজ ঘুরে বইয়ের নাম ও দাম বের করা
# ✅ প্রয়োজনীয় লাইব্রেরি ইমপোর্ট
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://books.toscrape.com/")
time.sleep(2)
while True:
# বই লোড করে
books = driver.find_elements(By.CLASS_NAME, "product_pod")
# বইয়ের নাম ও দাম প্রিন্ট
for book in books:
title = book.find_element(By.TAG_NAME, 'h3').text
price = book.find_element(By.CLASS_NAME, 'price_color').text
print(f"{title} - {price}")
# Next বাটনে ক্লিক করে পরের পেজে যাওয়া
try:
next_button = driver.find_element(By.CLASS_NAME, 'next')
next_page_link = next_button.find_element(By.TAG_NAME, 'a').get_attribute('href')
driver.get(next_page_link)
time.sleep(1)
except:
print("No more pages.")
break
driver.quit()
📗 টিউটোরিয়াল ৩: বিভিন্ন ক্যাটাগরির সব পেজ স্ক্র্যাপ করা (Pagination সহ)
লক্ষ্য: সব ক্যাটাগরি → প্রতিটা পেজ → প্রতিটা বইয়ের নাম ও দাম
# ✅ প্রয়োজনীয় লাইব্রেরি ইমপোর্ট
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://books.toscrape.com/")
time.sleep(2)
# ✅ সব ক্যাটাগরির লিংক বের করা
category_elements = driver.find_elements(By.CSS_SELECTOR, ".side_categories ul li ul li a")
category_links = [cat.get_attribute("href") for cat in category_elements]
# ✅ প্রতিটি ক্যাটাগরির জন্য স্ক্র্যাপিং শুরু
for link in category_links:
print(f"\n🔎 স্ক্র্যাপিং ক্যাটাগরি: {link}")
driver.get(link)
time.sleep(2)
while True:
books = driver.find_elements(By.CLASS_NAME, "product_pod")
for book in books:
title = book.find_element(By.TAG_NAME, 'h3').text
price = book.find_element(By.CLASS_NAME, 'price_color').text
print(f"{title} - {price}")
# পেজিনেশন আছে কিনা দেখি
try:
next_button = driver.find_element(By.CLASS_NAME, 'next')
next_page = next_button.find_element(By.TAG_NAME, 'a').get_attribute('href')
# নতুন পেজে যাওয়া
driver.get(next_page)
time.sleep(1)
except:
print("✅ ক্যাটাগরি শেষ")
break
driver.quit()
📦 কোন লাইব্রেরি ইনস্টল করতে হবে?
pip install selenium