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

পাইথনে BeautifulSoup দিয়ে ওয়েব স্ক্র্যাপিং বাস্তবায়ন (Implementing Web Scraping in Python with BeautifulSoup)

BeautifulSoup পাইথনের bs4 মডিউলের একটি ক্লাস। BeautifulSoup তৈরির মূল উদ্দেশ্য হল HTML বা XML ডকুমেন্ট পার্স করা।

bs4 (সংক্ষেপে BeautifulSoup) ইনস্টলেশন

Pip মডিউল ব্যবহার করে BeautifulSoup ইনস্টল করা খুব সহজ। কমান্ড শেলে নিচের কমান্ড চালান:

pip install bs4

উপরের কমান্ড টার্মিনালে চালালে আপনার স্ক্রিন কিছু এইরকম দেখাবে:

C:\Users\rajesh>pip install bs4 
Collecting bs4 
Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz 
Requirement already satisfied: beautifulsoup4 in c:\python\python361\lib\site-packages (from bs4) (4.6.0) 
Building wheels for collected packages: bs4 
Building wheel for bs4 (setup.py) ... done 
Stored in directory: C:\Users\rajesh\AppData\Local\pip\Cache\wheels\a0\b0\b2\4f80b9456b87abedbc0bf2d52235414c3467d8889be38dd472 
Successfully built bs4 
Installing collected packages: bs4 
Successfully installed bs4-0.0.1

BeautifulSoup সফলভাবে ইনস্টল হয়েছে কিনা ভেরিফাই করতে, একই টার্মিনালে নিচের কমান্ড চালান:

C:\Users\rajesh>python 
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) 
[MSC v.1900 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from bs4 import BeautifulSoup 
>>>

সফল, দারুন!

উদাহরণ ১

একটি HTML ডকুমেন্ট থেকে সব লিংক বের করা। ধরুন আমাদের কাছে একটি HTML ডকুমেন্ট আছে এবং আমরা ডকুমেন্টের সব রেফারেন্স লিংক কালেক্ট করতে চাই। তাই প্রথমে ডকুমেন্টটি একটি স্ট্রিং হিসেবে স্টোর করি নিচের মতো:

html_doc = ''' 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
'''

এখন উপরের ভেরিয়েবল html_doc কে BeautifulSoup-এর ইনিশিয়ালাইজার ফাংশনে পাস করে একটি সাপ অবজেক্ট তৈরি করি।

from bs4 import BeautifulSoup 
soup = BeautifulSoup(html_doc, 'html.parser')

এখন আমাদের কাছে সাপ অবজেক্ট আছে, আমরা এর ওপর BeautifulSoup ক্লাসের মেথডগুলো প্রয়োগ করতে পারি। এখন HTML ডকুমেন্টে দেয়া ট্যাগের সব অ্যাট্রিবিউট ও মান বের করা যাবে।

for tag in soup.find_all('a'): 
    print(tag.get('href'))

উপরের কোডে আমরা লুপ দিয়ে ডকুমেন্টের প্রতিটি <a> ট্যাগ পেতে চেষ্টা করছি এবং href অ্যাট্রিবিউট পাচ্ছি।

HTML ডকুমেন্ট স্ট্রিং থেকে সব লিংক বের করার সম্পূর্ণ কোড নিচে:

from bs4 import BeautifulSoup 
html_doc = ''' 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
''' 
soup = BeautifulSoup(html_doc, 'html.parser') 
for tag in soup.find_all('a'): 
    print(tag.get('href'))

রেজাল্ট

www.Tutorialspoint.com 
www.nseindia.com 
www.codesdope.com 
www.google.com 
www.facebook.com 
www.wikipedia.org 
www.twitter.com 
www.microsoft.com 
www.github.com 
www.nytimes.com 
www.youtube.com 
www.reddit.com 
www.python.org 
www.stackoverflow.com 
www.amazon.com 
www.rediff.com

উদাহরণ ২

একটি ওয়েবসাইট থেকে নির্দিষ্ট এলিমেন্ট (যেমন: python) যেসব লিংকেই আছে সেগুলো প্রিন্ট করা।

নিচের প্রোগ্রাম একটি নির্দিষ্ট ওয়েবসাইট থেকে যেসব URL-এ "python" শব্দ থাকে সেগুলো প্রিন্ট করবে।

from bs4 import BeautifulSoup 
from urllib.request import urlopen 
import re 
html = urlopen("https://www.python.org") 
content = html.read() 
soup = BeautifulSoup(content) 
for a in soup.findAll('a', href=True): 
    if re.findall('python', a['href']): 
        print("Python URL:", a['href'])

রেজাল্ট

Python URL: https://docs.python.org 
Python URL: https://pypi.python.org/ 
Python URL: https://www.facebook.com/pythonlang?fref=ts 
Python URL: https://brochure.getpython.info/ 
Python URL: https://docs.python.org/3/license.html 
Python URL: https://wiki.python.org/moin/BeginnersGuide 
Python URL: https://devguide.python.org/ 
Python URL: https://docs.python.org/faq/ 
Python URL: https://wiki.python.org/moin/Languages 
Python URL: https://python.org/dev/peps/ 
Python URL: https://wiki.python.org/moin/PythonBooks 
Python URL: https://wiki.python.org/moin/ 
Python URL: https://www.python.org/psf/codeofconduct/ 
Python URL: https://planetpython.org/ 
Python URL: /events/python-events 
Python URL: /events/python-user-group/ 
Python URL: /events/python-events/past/ 
Python URL: /events/python-user-group/past/ 
Python URL: https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event 
Python URL: //docs.python.org/3/tutorial/controlflow.html#defining-functions 
Python URL: //docs.python.org/3/tutorial/introduction.html#lists 
Python URL: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator 
Python URL: //docs.python.org/3/tutorial/ 
Python URL: //docs.python.org/3/tutorial/controlflow.html 
Python URL: /downloads/release/python-373/ 
Python URL: https://docs.python.org 
Python URL: //jobs.python.org 
Python URL: https://blog.python.org 
Python URL: https://feedproxy.google.com/~r/PythonInsider/~3/Joo0vg55HKo/python-373-is-now-available.html 
Python URL: https://feedproxy.google.com/~r/PythonInsider/~3/N5tvkDIQ47g/python-3410-is-now-available.html 
Python URL: https://feedproxy.google.com/~r/PythonInsider/~3/n0mOibtx6_A/python-3.html 
Python URL: /events/python-events/805/ 
Python URL: /events/python-events/817/ 
Python URL: /events/python-user-group/814/ 
Python URL: /events/python-events/789/ 
Python URL: /events/python-events/831/ 
Python URL: /success-stories/building-an-open-source-and-cross-platform-azure-cli-with-python/ 
Python URL: /success-stories/building-an-open-source-and-cross-platform-azure-cli-with-python/ 
Python URL: https://wiki.python.org/moin/TkInter 
Python URL: https://www.wxpython.org/ 
Python URL: https://ipython.org 
Python URL: #python-network 
Python URL: https://brochure.getpython.info/ 
Python URL: https://docs.python.org/3/license.html 
Python URL: https://wiki.python.org/moin/BeginnersGuide 
Python URL: https://devguide.python.org/ 
Python URL: https://docs.python.org/faq/ 
Python URL: https://wiki.python.org/moin/Languages 
Python URL: https://python.org/dev/peps/ 
Python URL: https://wiki.python.org/moin/PythonBooks 
Python URL: https://wiki.python.org/moin/ 
Python URL: https://www.python.org/psf/codeofconduct/ 
Python URL: https://planetpython.org/ 
Python URL: /events/python-events 
Python URL: /events/python-user-group/ 
Python URL: /events/python-events/past/ 
Python URL: /events/python-user-group/past/ 
Python URL: https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event 
Python URL: https://devguide.python.org/ 
Python URL: https://bugs.python.org/ 
Python URL: https://mail.python.org/mailman/listinfo/python-dev 
Python URL: #python-network 
Python URL: https://github.com/python/pythondotorg/issues 
Python URL: https://status.python.org/
  1. পাইথন এবং Scrapy দিয়ে ওয়েব স্ক্রেপিং

  2. পাইথনে lxml ব্যবহার করে ওয়েব স্ক্র্যাপিং বাস্তবায়ন

  3. পাইথনে ফোটোমোজেইক ইমপ্লিমেন্টেশন (Photomosaics)

  4. পাইথনে ওয়েব স্ক্র্যাপিং টুলস: urllib, Requests, BeautifulSoup, lxml, Selenium, MechanicalSoup