কম্পিউটার টিউটোরিয়াল

পাইথনে ওয়েব পেজ ক্রল করে সর্বাধিক ব্যবহৃত শব্দ বের করা


উদ্দেশ্য

একটি ওয়েব পেজ থেকে টেক্সট এক্সট্রাক্ট করে সবচেয়ে বেশি ব্যবহৃত শব্দগুলো বের করা।

প্রয়োজনীয় লাইব্রেরি

pip install requests beautifulsoup4

সম্পূর্ণ কোড

import requests
from bs4 import BeautifulSoup
from collections import Counter
import re

def start_crawl(url):
    word_list = []
    
    # ওয়েব পেজ আনা
    source_code = requests.get(url).text
    soup = BeautifulSoup(source_code, 'html.parser')
    
    # নির্দিষ্ট ক্লাসের div থেকে টেক্সট এক্সট্রাক্ট
    for each_text in soup.findAll('div', {'class': 'entry-content'}):
        content = each_text.text
        words = content.lower().split()
        word_list.extend(words)
    
    clean_wordlist(word_list)

def clean_wordlist(word_list):
    clean_list = []
    # অপ্রয়োজনীয় সিম্বল মুছে ফেলা
    symbols = r'[!@#$%^&*()_\-+=\{\}\[\]|\\;:"<>?/.,]'
    
    for word in word_list:
        # রেজেক্স দিয়ে সিম্বল রিমুভ
        cleaned = re.sub(symbols, '', word)
        if cleaned:  # খালি না হলে
            clean_list.append(cleaned)
    
    create_dictionary(clean_list)

def create_dictionary(clean_list):
    word_count = Counter(clean_list)
    # শীর্ষ ১০ শব্দ
    top_10 = word_count.most_common(10)
    print("Top 10 most frequent words:")
    for word, count in top_10:
        print(f"{word}: {count}")

# ড্রাইভার কোড
if __name__ == '__main__':
    start_crawl("https://www.tutorialspoint.com/python3/python_overview.htm")

কোডের মূল অংশ ব্যাখ্যা

  • requests.get(url).text — HTML কন্টেন্ট স্ট্রিং হিসেবে আনা।
  • BeautifulSoup(..., 'html.parser') — HTML পার্স করা।
  • findAll('div', {'class': 'entry-content'}) — নির্দিষ্ট কন্টেন্ট এরিয়া টার্গেট করা।
  • re.sub(symbols, '', word) — রেজেক্স দিয়ে পাঙ্কচুয়েশন/সিম্বল মুছে ফেলা।
  • Counter.most_common(10) — সবচেয়ে বেশি ফ্রিকোয়েন্সি والے ১০টি শব্দ।

উন্নতির সুযোগ

  • স্টপ ওয়ার্ড (the, and, is ইত্যাদি) ফিল্টার করা।
  • লেমাটাইজেশন/স্টেমিং ব্যবহার করা।
  • এরর হ্যান্ডলিং (timeout, status code চেক) যোগ করা।
  1. পাইথন প্রোগ্রামে try এবং except

  2. পাইথন প্রোগ্রাম সেন্টেন্সে ওয়ার্ড কাউন্ট করা

  3. পাইথনে স্ট্রিং-এ সবচেয়ে বেশি বার আসা ক্যারেক্টার এবং তার কাউন্ট বের করা

  4. পাইথনে ডেটা সেট থেকে k সবচেয়েكثر ফ্রিকোয়েন্ট শব্দ বের করা