← Back to Python Course | Chapter 13: Data Science & Web | Lesson 11 of 14

Python वेब स्क्रैपिंग की मूल बातें

Web scraping किसी program से web page पढ़वाकर उसमें से चाहिए हिस्से निकलवाना है, जैसे newspaper पर highlighter चलाना। यह हाथ से copy करने से बचाता है।

Web Scraping क्या है?

Web scraping का मतलब है किसी page की raw HTML को programmatically fetch करना और फिर उस markup को parse करके ज़रूरी specific data निकालना, न कि उसे browser में visually पढ़ना।

यह tutorial parsing के लिए BeautifulSoup इस्तेमाल करता है -- पहले इसे pip install beautifulsoup4 से install करें।

उदाहरण: What is Web Scraping?

python
# pip install beautifulsoup4
from bs4 import BeautifulSoup

html = "<html><body><h1>Title</h1></body></html>"
soup = BeautifulSoup(html, "html.parser")
print(soup.h1.text)

Tags और Titles ढूँढना

BeautifulSoup का .find(tag_name) parsed document में कहीं भी पहला matching HTML element ढूँढता है और आपको उसके text और attributes तक पहुँच देता है, जो किसी page की main heading या title जैसी चीज़ें निकालने का basic building block है।

उदाहरण: Finding Tags and Titles

python
from bs4 import BeautifulSoup

html = "<html><head><title>My Page</title></head></html>"
soup = BeautifulSoup(html, "html.parser")
print(soup.find("title").text)  # locates the first <title> element and reads its text

Classes से Filter करना

चूँकि असली web pages एक ही tag साझा करने वाले elements में फ़र्क़ करने के लिए काफी हद तक CSS classes पर निर्भर होते हैं, .find(tag, class_=name) आपको search को किसी specific class वाले elements तक सीमित करने देता है -- article titles या price tags जैसे structured content को scrape करने के लिए ज़रूरी, जो किसी tag name को unrelated elements के साथ साझा करते हैं।

उदाहरण: Filtering with Classes

python
from bs4 import BeautifulSoup

html = '<div class="price">$10</div><div>Other</div>'
soup = BeautifulSoup(html, "html.parser")
print(soup.find("div", class_="price").text)  # narrows the search to a specific CSS class

कई Elements ढूँढना

.find_all(tag) .find() की तरह पहले match पर रुकने की बजाय, page पर हर matching element को एक list के रूप में लौटाता है।

जब भी आपको किसी चीज़ के सारे instances चाहिए हों -- हर link, हर list item, किसी table की हर row -- सिर्फ एक नहीं, तब इसे इस्तेमाल करें।

उदाहरण: Finding Multiple Elements

python
from bs4 import BeautifulSoup

html = "<ul><li>One</li><li>Two</li></ul>"
soup = BeautifulSoup(html, "html.parser")
items = soup.find_all("li")  # returns every matching element, not just the first
print([item.text for item in items])

Link Attributes निकालना

Links <a> tags में होते हैं, और असली destination URL tag के href attribute में store होता है, जिसे dictionary key की तरह access किया जाता है: link[href]

यह भूल जाना और link के visible text को ऐसे पढ़ने की कोशिश करना जैसे वो URL हो, एक आम scraping गलती है।

उदाहरण: Extracting Link Attributes

python
from bs4 import BeautifulSoup

html = '<a href="https://example.com">Visit</a>'
soup = BeautifulSoup(html, "html.parser")
link = soup.find("a")
print(link["href"])  # the destination URL lives in the href attribute
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.