কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েড অ্যাপ্লিকেশনে ওয়েব স্ক্র্যাপিং করার পদ্ধতি

Jsoup লাইব্রেরি ব্যবহার করে ওয়েব স্ক্র্যাপিং

এই উদাহরণে Jsoup লাইব্রেরি ব্যবহার করে একটি ওয়েবসাইট থেকে টেক্সট কনটেন্ট এক্সট্র্যাক্ট করার পদ্ধতি দেখানো হয়েছে।

প্রথম ধাপ: প্রজেক্ট তৈরি করুন

Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।

দ্বিতীয় ধাপ: Jsoup ডিপেন্ডেন্সি যোগ করুন

build.gradle (Module: app) ফাইলে নিচের লাইনটি যোগ করুন:

implementation 'org.jsoup:jsoup:1.11.2'

সিঙ্ক করার পর লাইব্রেরি ব্যবহারmeye যাবে।

তৃতীয় ধাপ: লেআউট ফাইল

res/layout/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"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:text=""
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:padding="4dp"
        android:layout_centerHorizontal="true" />
    <Button
        android:id="@+id/btnView"
        android:text="Scrap Text from web"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25sp"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

চতুর্থ ধাপ: মেইন এক্টিভিটিতে AsyncTask ব্যবহার করে স্ক্র্যাপিং

src/MainActivity.java - নেটওয়ার্ক অপারেশন ব্যাকগ্রাউন্ড থ্রেডে করতে হয়:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.btnView);
        
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new doIT().execute();
            }
        });
    }
    
    // AsyncTask برای ব্যাকগ্রাউন্ডে নেটওয়ার্ক কল
    public class doIT extends AsyncTask<Void, Void, Void> {
        String words;
        
        @Override
        protected Void doInBackground(Void... params) {
            try {
                // Jsoup দিয়ে ওয়েবসাইট কানেক্ট করে HTML পার্স করা
                Document document = Jsoup.connect("https://www.tutorialspoint.com/css_online_training/index.asp").get();
                words = document.text(); // শুধু টেক্সট কনটেন্ট বের করা
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            textView.setText(words); // UI থ্রেডে ফলাফল দেখানো
        }
    }
}

পঞ্চম ধাপ: ইন্টারনেট পারমিশন এবং ম্যানিফেস্ট

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <uses-permission android:name="android.permission.INTERNET"/>
    <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>

গুরুত্বপূর্ণ নোট

  • INTERNET পারমিশন অবশ্য দিতে হবে
  • নেটওয়ার্ক অপারেশন মেইন থ্রেডে করা যাবে না - AsyncTask, Thread, বা Coroutines ব্যবহার করুন
  • আধুনিক অ্যান্ড্রয়েড ডেভেলপমেন্টে AsyncTask ডিপ্রিকেটেড, বরং Coroutines + Retrofit/OkHttp বা RxJava ব্যবহার করুন
  • ওয়েবসাইটের robots.txt এবং Terms of Service চেক করে স্ক্র্যাপিং করুন

অ্যাপ্লিকেশন চালান

বাটনে ক্লিক করলে ওয়েবসাইট থেকে টেক্সট কনটেন্ট লোড হবে।

অ্যান্ড্রয়েড অ্যাপ্লিকেশনে ওয়েব স্ক্র্যাপিং করার পদ্ধতি

  1. অ্যান্ড্রয়েড অ্যাপ্লিকেশনে বর্তমান তারিখ ও সময় প্রদর্শন করার পদ্ধতি

  2. অ্যান্ড্রয়েড অ্যাপ থেকে ওয়েব ব্রাউজারে ওয়েবসাইট খোলার পদ্ধতি

  3. অ্যান্ড্রয়েডে লাইন আঁকা (Canvas এবং Paint ব্যবহার করে)

  4. Android-এ প্রোগ্রামেটিক্যালি অ্যাপ্লিকেশন বন্ধ করা