투케이2K

88. (AndroidStudio/android/java) jsoup 라이브러리 사용해 구글 플레이스토어 앱 버전 크롤링 실시 - HTML 크롤링 본문

Android

88. (AndroidStudio/android/java) jsoup 라이브러리 사용해 구글 플레이스토어 앱 버전 크롤링 실시 - HTML 크롤링

투케이2K 2021. 3. 18. 13:09

/* =========================== */

[ 개발 환경 설정 ]

개발 툴 : AndroidStudio

개발 언어 : java

/* =========================== */

/* =========================== */

[소스 코드]

 

//========== [build.gradle(Project) 파일] ==========
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

        // ==== 추가 ====
        maven { url "https://jitpack.io" }

        // ==== 추가 ====
        mavenCentral()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}






//========== [build.gradle(Module:app) 파일] ==========
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "kr.co.2k.myapplication"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //==== HTML 크롤링 하기 위함 ====
    implementation 'org.jsoup:jsoup:1.12.1'

}






//========== [AndroidManifest.xml 파일] ==========
<uses-permission android:name="android.permission.INTERNET"/>






//========== [java - MarketVersionChecker 호출] ==========
new MarketVersionChecker().getMarketVersion("com.lguplus.mobile.cs"); 






//========== [java - MarketVersionChecker 정의] ==========
class MarketVersionChecker {
	public void getMarketVersion(final String packages) {
		new Thread(){
			@Override
			public void run(){
				try {
					String versionData = "";
					//TODO ==== [작업 내용] ====
					Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id=" + packages).get();
					//Elements Version = doc.select(".htlgb").eq(3);
					Elements Version = doc.select(".htlgb").eq(7);
					for (Element mElement : Version) {
						versionData =  mElement.text().trim();
						Log.d("---","---");
						Log.w("//===========//","================================================");
						Log.d("","\n"+"[MarketVersionChecker > 구글스토어 앱 버전 확인 실시]");
						Log.d("","\n"+"[패키지명 : "+String.valueOf(packages)+"]");
						Log.d("","\n"+"[버전 : "+String.valueOf(versionData)+"]");
						Log.w("//===========//","================================================");
						Log.d("---","---");
						//TODO ==== [UI 변경 작업 실시] ====
						final String finalVersionData = versionData;
						runOnUiThread(new Runnable(){
							@Override
							public void run() {
								Toast.makeText(getApplication(),"버전 확인 성공 : "+ finalVersionData,Toast.LENGTH_SHORT).show();
							}
						});
					}
				}
				catch (Exception e) {
					Log.d("---","---");
					Log.e("//===========//","================================================");
					Log.d("","\n"+"[MarketVersionChecker > 구글스토어 앱 버전 확인 실패]");
					Log.d("","\n"+"[패키지명 : "+String.valueOf(packages)+"]");
					Log.d("","\n"+"[버전 : "+String.valueOf("")+"]");
					Log.e("//===========//","================================================");
					Log.d("---","---");
					//TODO ==== [UI 변경 작업 실시] ====
					runOnUiThread(new Runnable(){
						@Override
						public void run() {
							Toast.makeText(getApplication(),"버전 확인 실패 ... ",Toast.LENGTH_SHORT).show();
						}
					});
					e.printStackTrace();
				}
			}
		}.start();
	}
}

/* =========================== */

[결과 출력]

/* =========================== */

반응형
Comments