BeautifulSoup पायथन के bs4 मॉड्यूल में एक क्लास है। BeautifulSoup बनाने का मूल उद्देश्य HTML या XML दस्तावेजों को पार्स करना है।
bs4 इंस्टॉल करना (संक्षेप में BeautifulSoup)
pip मॉड्यूल का उपयोग करके beautifulsoup को इंस्टॉल करना आसान है। बस अपने कमांड शेल पर नीचे दी गई कमांड चलाएं।
pip install bs4
अपने टर्मिनल पर उपरोक्त कमांड चलाने पर, आपकी स्क्रीन कुछ इस तरह दिखाई देगी -
C:\Users\rajesh>pip install bs4\nCollecting bs4\nDownloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz\nRequirement already satisfied: beautifulsoup4 in c:\python\python361\lib\site-packages (from bs4) (4.6.0)\nBuilding wheels for collected packages: bs4\nBuilding wheel for bs4 (setup.py) ... done\nStored in directory: C:\Users\rajesh\AppData\Local\pip\Cache\wheels\a0\b0\b2\4f80b9456b87abedbc0bf2d52235414c3467d8889be38dd472\nSuccessfully built bs4\nInstalling collected packages: bs4\nSuccessfully installed bs4-0.0.1
सत्यापित करने के लिए कि BeautifulSoup आपके मशीन में सफलतापूर्वक इंस्टॉल हुआ है या नहीं, उसी टर्मिनल में नीचे दी गई कमांड चलाएं:
C:\Users\rajesh>python\nPython 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32\nType "help", "copyright", "credits" or "license" for more information.\n>>> from bs4 import BeautifulSoup\n>>>
सफल, बढ़िया!
उदाहरण 1
HTML दस्तावेज़ से सभी लिंक ढूंढें। अब मान लीजिए हमारे पास एक HTML दस्तावेज़ है और हम दस्तावेज़ में सभी रेफरेंस लिंक एकत्र करना चाहते हैं। तो पहले हम दस्तावेज़ को नीचे की तरह एक स्ट्रिंग के रूप में संग्रहीत करेंगे:
html_doc = ''' '''
अब हम html_doc वेरिएबल को BeautifulSoup के इनिशियलाइज़र फ़ंक्शन में पास करके एक सूप ऑब्जेक्ट बनाएंगे।
from bs4 import BeautifulSoup\nsoup = BeautifulSoup(html_doc, 'html.parser')
अब हमारे पास सूप ऑब्जेक्ट है, हम इस पर BeautifulSoup क्लास के मेथड लागू कर सकते हैं। अब हम html_doc में दिए गए टैग के सभी एट्रिब्यूट्स और उनके मान ढूंढ सकते हैं।
for tag in soup.find_all('a'):\n print(tag.get('href'))उपरोक्त कोड से हम लूप के माध्यम से दस्तावेज़ में हर को प्राप्त करके और href एट्रिब्यूट प्राप्त करके html_doc स्ट्रिंग में सभी लिंक प्राप्त करने का प्रयास कर रहे हैं।
html_doc स्ट्रिंग से सभी लिंक प्राप्त करने के लिए हमारा पूरा कोड नीचे है।
from bs4 import BeautifulSoup\nhtml_doc = ''' '''\nsoup = BeautifulSoup(html_doc, 'html.parser')\nfor tag in soup.find_all('a'):\n print(tag.get('href'))
परिणाम
www.Tutorialspoint.com\nwww.nseindia.com\nwww.codesdope.com\nwww.google.com\nwww.facebook.com\nwww.wikipedia.org\nwww.twitter.com\nwww.microsoft.com\nwww.github.com\nwww.nytimes.com\nwww.youtube.com\nwww.reddit.com\nwww.python.org\nwww.stackoverflow.com\nwww.amazon.com\nwww.rediff.com
उदाहरण 2
एक वेबसाइट से विशिष्ट एलिमेंट (उदाहरण के लिए: python) वाले सभी लिंक प्रिंट करें।
नीचे दिया गया प्रोग्राम एक विशिष्ट वेबसाइट से उन सभी URL को प्रिंट करेगा जिनके लिंक में "python" है।
from bs4 import BeautifulSoup\nfrom urllib.request import urlopen\nimport re\n\nhtml = urlopen("https://www.python.org")\ncontent = html.read()\nsoup = BeautifulSoup(content)\nfor a in soup.findAll('a', href=True):\n if re.findall('python', a['href']):\n print("Python URL:", a['href'])परिणाम
Python URL: https://docs.python.org\nPython URL: https://pypi.python.org/\nPython URL: https://www.facebook.com/pythonlang?fref=ts\nPython URL: https://brochure.getpython.info/\nPython URL: https://docs.python.org/3/license.html\nPython URL: https://wiki.python.org/moin/BeginnersGuide\nPython URL: https://devguide.python.org/\nPython URL: https://docs.python.org/faq/\nPython URL: https://wiki.python.org/moin/Languages\nPython URL: https://python.org/dev/peps/\nPython URL: https://wiki.python.org/moin/PythonBooks\nPython URL: https://wiki.python.org/moin/\nPython URL: https://www.python.org/psf/codeofconduct/\nPython URL: https://planetpython.org/\nPython URL: /events/python-events\nPython URL: /events/python-user-group/\nPython URL: /events/python-events/past/\nPython URL: /events/python-user-group/past/\nPython URL: https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event\nPython URL: //docs.python.org/3/tutorial/controlflow.html#defining-functions\nPython URL: //docs.python.org/3/tutorial/introduction.html#lists\nPython URL: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator\nPython URL: //docs.python.org/3/tutorial/\nPython URL: //docs.python.org/3/tutorial/controlflow.html\nPython URL: /downloads/release/python-373/\nPython URL: https://docs.python.org\nPython URL: //jobs.python.org\nPython URL: https://blog.python.org\nPython URL: https://feedproxy.google.com/~r/PythonInsider/~3/Joo0vg55HKo/python-373-is-now-available.html\nPython URL: https://feedproxy.google.com/~r/PythonInsider/~3/N5tvkDIQ47g/python-3410-is-now-available.html\nPython URL: https://feedproxy.google.com/~r/PythonInsider/~3/n0mOibtx6_A/python-3.html\nPython URL: /events/python-events/805/\nPython URL: /events/python-events/817/\nPython URL: /events/python-user-group/814/\nPython URL: /events/python-events/789/\nPython URL: /events/python-events/831/\nPython URL: /success-stories/building-an-open-source-and-cross-platform-azure-cli-with-python/\nPython URL: /success-stories/building-an-open-source-and-cross-platform-azure-cli-with-python/\nPython URL: https://wiki.python.org/moin/TkInter\nPython URL: https://www.wxpython.org/\nPython URL: https://ipython.org\nPython URL: #python-network\nPython URL: https://brochure.getpython.info/\nPython URL: https://docs.python.org/3/license.html\nPython URL: https://wiki.python.org/moin/BeginnersGuide\nPython URL: https://devguide.python.org/\nPython URL: https://docs.python.org/faq/\nPython URL: https://wiki.python.org/moin/Languages\nPython URL: https://python.org/dev/peps/\nPython URL: https://wiki.python.org/moin/PythonBooks\nPython URL: https://wiki.python.org/moin/\nPython URL: https://www.python.org/psf/codeofconduct/\nPython URL: https://planetpython.org/\nPython URL: /events/python-events\nPython URL: /events/python-user-group/\nPython URL: /events/python-events/past/\nPython URL: /events/python-user-group/past/\nPython URL: https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event\nPython URL: https://devguide.python.org/\nPython URL: https://bugs.python.org/\nPython URL: https://mail.python.org/mailman/listinfo/python-dev\nPython URL: #python-network\nPython URL: https://github.com/python/pythondotorg/issues\nPython URL: https://status.python.org/