एंड्रॉइड में Jsoup के साथ वेब स्क्रैपिंग
यह उदाहरण दर्शाता है कि Jsoup लाइब्रेरी का उपयोग करके वेब पेज से डेटा कैसे निकाला जाए। नेटवर्क ऑपरेशन मुख्य थ्रेड पर नहीं किए जा सकते, इसलिए AsyncTask (लीगेसी) या Coroutines (आधुनिक) का उपयोग करें।
निर्भरता जोड़ें (build.gradle)
dependencies {
implementation "org.jsoup:jsoup:1.17.2"
// कोरूटीन के लिए
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
}
चरण 1: नया प्रोजेक्ट बनाएं
एंड्रॉइड स्टूडियो में File → New Project चुनें।
चरण 2: लेआउट (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@id/btnView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:padding="8dp"
android:text=""
android:textSize="14sp" />
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="वेब से टेक्स्ट निकालें" />
</RelativeLayout>
चरण 3: MainActivity.kt (आधुनिक कोरूटीन दृष्टिकोण)
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.jsoup.Jsoup
import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "Web Scraping Demo"
textView = findViewById(R.id.textView)
button = findViewById(R.id.btnView)
button.setOnClickListener {
scrapeWebsite()
}
}
private fun scrapeWebsite() {
button.isEnabled = false
textView.text = "लोड हो रहा है..."
lifecycleScope.launch {
try {
// IO डिस्पैचर पर नेटवर्क कॉल
val document = withContext(Dispatchers.IO) {
Jsoup.connect("https://www.tutorialspoint.com/css_online_training/index.asp")
.userAgent("Mozilla/5.0 (Linux; Android 10)") // यूजर एजेंट सेट करें
.timeout(10000) // 10 सेकंड टाइमआउट
.get()
}
// मुख्य थ्रेड पर UI अपडेट
val text = document.text()
textView.text = if (text.isNotEmpty()) text else "कोई डेटा नहीं मिला"
} catch (e: IOException) {
textView.text = "त्रुटि: ${e.message}"
Toast.makeText(this, "नेटवर्क त्रुटि: ${e.message}", Toast.LENGTH_LONG).show()
} catch (e: Exception) {
textView.text = "अप्रत्याशित त्रुटि: ${e.message}"
} finally {
button.isEnabled = true
}
}
}
}
चरण 4: AndroidManifest.xml (इंटरनेट अनुमति आवश्यक)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.webscraping">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
उन्नत Jsoup उपयोग:
// विशिष्ट तत्वों का चयन
val title = document.select("h1").first()?.text()
val links = document.select("a[href]").map { it.attr("abs:href") }
val images = document.select("img[src]").map { it.attr("abs:src") }
// टेबल डेटा निकालना
val rows = document.select("table tr")
for (row in rows) {
val cells = row.select("td")
val cellData = cells.map { it.text() }
println(cellData)
}
// फॉर्म सबमिशन
val form = document.select("form").first()
val response = Jsoup.connect(form.absUrl("action"))
.data(form.formData())
.post()
सर्वोत्तम अभ्यास:
- कोरूटीन का उपयोग करें: AsyncTask अप्रचलित है
- टाइमआउट सेट करें: अनिश्चित हैंगिंग से बचें
- यूजर एजेंट: कुछ साइट्स डिफॉल्ट Jsoup एजेंट को ब्लॉक करती हैं
- त्रुटि प्रबंधन: नेटवर्क विफलताओं को उचित तरीके से संभालें
- कानूनी अनुपालन: robots.txt और सेवा की शर्तों का सम्मान करें
- कैशिंग: अनावश्यक अनुरोधों से बचने के लिए प्रतिक्रियाओं को कैश करें