Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

เว็บสแครปปิ้งในแอปพลิเคชัน Android ด้วย Jsoup

ภาพรวม

ตัวอย่างนี้สาธิตวิธีทำ Web Scraping ใน Android โดยใช้ไลบรารี Jsoup เพื่อดึงข้อมูลจากเว็บไซต์

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: เพิ่ม Dependency (build.gradle Module:app)

dependencies {
    implementation 'org.jsoup:jsoup:1.11.2'
    // หรือใช้เวอร์ชันใหม่กว่า เช่น 1.17.2
}

ขั้นตอนที่ 3: เลย์เอาต์ (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:layout_width="match_parent"
        android:layout_height="600dp"
        android:padding="4dp"
        android:layout_centerHorizontal="true"
        android:text="" />

    <Button
        android:id="@+id/btnView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scrap Text from web"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="25sp" />

</RelativeLayout>

ขั้นตอนที่ 4: โค้ดหลัก (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(v -> new DoItTask().execute());
    }

    // ใช้ AsyncTask เพื่อทำงาน Network ใน Background Thread
    private class DoItTask extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... params) {
            try {
                Document document = Jsoup.connect(
                    "https://www.tutorialspoint.com/css_online_training/index.asp").get();
                return document.text(); // ดึงข้อความทั้งหมดจากหน้าเว็บ
            } catch (IOException e) {
                e.printStackTrace();
                return "Error: " + e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            textView.setText(result);
        }
    }
}

ขั้นตอนที่ 5: AndroidManifest.xml (เพิ่มสิทธิ์ INTERNET)

<?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>

การทดสอบ

รันแอปพลิเคชัน กดปุ่ม "Scrap Text from web" จะดึงข้อความจากเว็บไซต์เป้าหมายมาแสดง

เว็บสแครปปิ้งในแอปพลิเคชัน Android ด้วย Jsoup

หมายเหตุสำคัญ

  • ต้องทำ Network Operation ใน Background Thread (AsyncTask, Coroutines, RxJava)
  • ต้องขอสิทธิ์ INTERNET ใน Manifest
  • ควรเพิ่ม Error Handling และ Timeout
  • ปัจจุบันแนะนำให้ใช้ Kotlin Coroutines หรือ Retrofit แทน AsyncTask ที่ Deprecated แล้ว