Python वेब स्क्रैपिंग की मूल बातें
In this page:
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?
# 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
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
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
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
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
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB