Automating Internet with Excel VBA in Hindi
Makhanlal Chaturvedi University / BCA / VBA programming
Automating Internet Tasks using Excel VBA in Hindi
Automating Internet with Excel VBA in Hindi
Introduction to Automating Internet with Excel VBA
Excel VBA का उपयोग सिर्फ spreadsheet tasks को automate करने तक सीमित नहीं है, बल्कि इसके जरिए आप इंटरनेट से जुड़ी गतिविधियों को भी control कर सकते हैं। इससे आप वेबसाइट खोल सकते हैं, उनमें से जानकारी निकाल सकते हैं, और उस data को Excel में import करके उपयोग कर सकते हैं। यह लेख एक beginner-friendly गाइड है, जिसमें हम step-by-step सीखेंगे कि Excel VBA से इंटरनेट कैसे automate करें।
Using VBA to open and browse websites in Excel in Hindi
- Excel VBA में वेबसाइट खोलने के लिए सबसे पहले हमें Internet Explorer के साथ interaction करना पड़ता है।
-
इसके लिए हम VBA में एक object बनाते हैं जिसका नाम होता है
InternetExplorer.Application। -
नीचे एक उदाहरण दिया गया है, जिसमें Google को Excel VBA से खोला गया है:
Sub OpenGoogle()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.google.com"
End Sub -
IE.Visible = Trueका मतलब है कि Internet Explorer का window user को दिखेगा। -
IE.Navigatemethod का उपयोग किसी भी वेबसाइट को खोलने के लिए किया जाता है।
Extracting data from Internet Explorer using Excel VBA in Hindi
- वेबसाइट खुलने के बाद हम HTML document को read करके उसमें से data extract कर सकते हैं।
-
इसके लिए हमें HTML elements को पहचानना होता है, जैसे कि
getElementById,getElementsByTagNameयाgetElementsByClassNameका उपयोग। -
उदाहरण के लिए, यदि हमें किसी paragraph का content निकालना है:
Sub ExtractParagraph()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://example.com"
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
Dim doc As Object
Set doc = IE.Document
MsgBox doc.getElementsByTagName("p")(0).innerText
End Sub -
ऊपर दिए गए कोड में पहला
<p>टैग का content message box में दिखाया गया है।
Web scraping basics with Excel VBA in Hindi
- Web Scraping का मतलब होता है किसी वेबसाइट के पेज से जरूरी जानकारी निकालना।
- Excel VBA में हम Web Scraping के लिए Internet Explorer का उपयोग करते हैं क्योंकि यह एक COM supported browser है।
- scraping से पहले हमें website का structure (HTML layout) समझना होता है जिससे हमें पता चलता है कि हमें कौन से elements scrape करने हैं।
-
Web scraping की बेसिक स्टेप्स:
- Internet Explorer object create करना
- वेबसाइट को navigate करना
- loading complete होने तक wait करना
- HTML elements को access करना
- data को Excel में paste करना
-
एक उदाहरण जिसमें किसी वेबसाइट के सभी links को Excel में निकाला गया है:
Sub ExtractLinks()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "https://example.com"
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
Dim doc As Object
Set doc = IE.Document
Dim allLinks As Object
Set allLinks = doc.getElementsByTagName("a")
Dim i As Integer
For i = 0 To allLinks.Length - 1
Cells(i + 1, 1).Value = allLinks(i).href
Next i
End Sub - ऊपर का code Excel sheet के column A में सभी links को list कर देगा।
Exporting scraped data to Excel sheets in Hindi
- जब scraping complete हो जाए, तो data को Excel के cells में store करना होता है।
- हम loop के माध्यम से हर element को Excel sheet के अलग-अलग rows/columns में paste कर सकते हैं।
-
उदाहरण के लिए, अगर किसी वेबसाइट से product name और price निकालना है:
Sub ExtractProductDetails()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "https://example.com/products"
Do While IE.Busy Or IE.ReadyState <> 4
DoEvents
Loop
Dim doc As Object
Set doc = IE.Document
Dim products, prices As Object
Set products = doc.getElementsByClassName("product-name")
Set prices = doc.getElementsByClassName("product-price")
Dim i As Integer
For i = 0 To products.Length - 1
Cells(i + 1, 1).Value = products(i).innerText
Cells(i + 1, 2).Value = prices(i).innerText
Next i
End Sub - इस code में सभी product names और उनकी prices Excel sheet में column A और B में आ जाएंगी।
Important Tips for Beginners
- Internet Explorer अब deprecated है, इसलिए VBA web scraping केवल internal tools या पुराने websites पर ही करें।
- Internet Explorer आधारित scraping केवल उन्हीं websites पर काम करता है जो बिना JavaScript rendering के भी data दिखा देती हैं।
- VBA का
Setkeyword object assign करने के लिए होता है, इसे कभी ना भूलें। - Internet connectivity active होनी चाहिए scraping से पहले।
Table Example for Output in Excel
| Product Name | Price |
|---|---|
| Product A | ₹500 |
| Product B | ₹750 |
FAQs
Excel VBA में वेबसाइट खोलने के लिए
InternetExplorer.Application object का उपयोग किया जाता है। आप IE.Navigate "https://example.com" का उपयोग करके वेबसाइट ओपन कर सकते हैं।
हां, Excel VBA में
IE.Document के माध्यम से आप HTML elements को access करके data extract कर सकते हैं, जैसे कि getElementsByTagName या getElementById से।
Web Scraping का मतलब है किसी वेबसाइट के HTML elements से जानकारी निकालना। Excel VBA में इसे Internet Explorer को control करके और HTML document से जरूरी data extract करके किया जाता है।
Extract किए गए data को Excel sheet में
Cells(row, column).Value = data की मदद से लिखा जाता है। हर item को loop में डालकर अलग-अलग cells में रखा जा सकता है।
Excel VBA में आमतौर पर Internet Explorer का उपयोग किया जाता है क्योंकि यह VBA के साथ COM object के रूप में काम करता है। हालांकि, यह अब deprecated हो चुका है।
हां, जब आप किसी वेबसाइट को Excel VBA से खोलते या scrape करते हैं, तो active internet connection जरूरी होता है ताकि वेबसाइट का data load हो सके।