Eclipse 2023-09
Note: For personal reference using Jsoup in java
- create a new project
- under project root create folder "lib"
- download Jsoup binary (jar file) from https://jsoup.org/download and put in folder "lib". Use the latest one, this example use jsoup-1.17.2.jar
- in project properties -> Java Build Path -> Libraries -> Modulepath -> Add external JARs and add your Jsoup binary, Apply and Close
Java code to extract element in table from my blog https://dedetoknotes.blogspot.com/2024/01/my-radio-list.html
package com.dedetok;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Main {
public static void main(String[] args) {
try {
Document doc = Jsoup.connect("https://dedetoknotes.blogspot.com/2024/01/my-radio-list.html").get();
if (doc!=null) {
System.out.println("Ok: "+doc.title()); //debug
//Elements myTable = doc.getElementsByTag("table");
Element myTable = doc.getElementById("com.dedetok.myradiolist");
//System.out.println("size children: "+myTable.childrenSize()); //debug
Elements myTr = myTable.select("tr");
System.out.println("number of tr: "+myTr.size());
//System.out.println(myTr.toString()); //debug
// iterate row tr
for (Element e : myTr) {
Elements myTd = e.select("td");
//System.out.println("number of td: "+myTd.size()); //debug
// iterate column td
for (Element eTd: myTd) {
//get icon url in image or text
Element myImg = eTd.select("img").first();
if (myImg!=null) {
String myIconUrl = myImg.absUrl("src");
System.out.print(myIconUrl+"|"+eTd.text()+" ");
} else {
System.out.print(eTd.text()+";");
}
}
System.out.println(); // add a new line
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
For Android Studio 2023.1.1 Patch 1
add build.gradle.kts (Module :app)
dependencies {
...
implementation("org.jsoup:jsoup:1.17.2") // jsoup
}
No comments:
Post a Comment