jsoup là một thư viện mạnh mẽ được sử dụng nhiều trong Android để phân tích các phần HTML thành các đối tượng trong Java Android
Cài đặt thư viện jsoup vào project Android
Có thể tham khảo nền tảng chủ sở hữu của jsoup trước khi bắt đầu. https. //jsoup. tổ chức/
Create project Android set name is JsoupDemo
Để cài đặt thư viện vào dự án vừa tạo, dán dòng dưới đây vào
Jsoup.parse[String html]7 trong bản dựng tệp. lớp [Mô-đun. ứng dụng]
compile 'org.jsoup:jsoup:1.9.2'
Sau đó nhấn Sync Now để Android Studio tự động load thư viện vào project, đến đây xem như cài đặt jsoup thành công thư viện.
Create Document from data source
Để tạo đối tượng
Jsoup.parse[String html]8 bằng jsoup có rất nhiều cách và từ rất nhiều nguồn dữ liệu khác nhau
- Từ 1 chuỗi có đủ định dạng tài liệu HTML
- From 1 string has a format 1 section of the HTML document
- From 1 path to page web
Cách 1. tạo tài liệu từ chuỗi
Jsoup.parse[String html]
Phương thức
Jsoup.parse[String html]9 Trả về 1 đối tượng là
Jsoup.parse[String html]8 từ 1
String html = "First parse1 có định dạng HTML đầy đủParses the input HTML into a new Document.
"; Document doc = Jsoup.parse[html];
String html = "First parseParses the input HTML into a new Document.
"; Document doc = Jsoup.parse[html];
cách 2. tạo tài liệu từ 1 phần nội dung
________số 8_______Phương thức
String html = "First parse2 trả về 1 đối tượng làParses the input HTML into a new Document.
"; Document doc = Jsoup.parse[html];
Jsoup.parse[String html]8 từ 1
String html = "First parse1 là 1 phần của HTMLParses the input HTML into a new Document.
"; Document doc = Jsoup.parse[html];
String html = ".
"; Document doc = Jsoup.parseBodyFragment[html]; Element body = doc.body[];Cách 3. tạo tài liệu từ url
Jsoup.connect[String url]Phương thức
String html = "First parse5 trả về 1 đối tượng làParses the input HTML into a new Document.
"; Document doc = Jsoup.parse[html]; Jsoup.parse[String html]8 từ 1 trang webĐể tạo
Jsoup.parse[String html]8 từ url, Android phải tải nội dung của trang web thông qua mạng. Vì thế, nếu sử dụng cách thứ ba thì không được đặt trong chuỗi giao diện người dùng mà phải tạo chuỗi khác để xử lý ví dụ như AsyncTask,Document doc = Jsoup.connect["//training.stdio.vn/"].get[]; String title = doc.title[];Trong phần phân tích dữ liệu từ
Jsoup.parse[String html]8, được giới thiệu ở phía dưới cũng sẽ sử dụng cách nàyCách 4. tạo tài liệu từ tập tin
Jsoup.parse[File in, String charsetName, String baseUri]Phương thức
String html = "First parse9 trả về 1 đối tượng làParses the input HTML into a new Document.
"; Document doc = Jsoup.parse[html];Jsoup.parse[String html]8 từ 1 tập tin [file]File file = new File["/tmp/eitguide.html"]; Document doc = Jsoup.parse[file, "UTF-8"];Data split data from Document
Trong ví dụ thực thi này, sẽ lấy thông tin về các khóa học lập trình trong trang https. //tập huấn. stdio. vn/training bao gồm
- The key key learning
- description
- học phí
- Thời lượng học
- Hình đại diện
package com.example.nguyennghia.jsoupdemo; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import javax.security.auth.login.LoginException; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final String URL = "//training.stdio.vn/training"; @Override protected void onCreate[Bundle savedInstanceState] { super.onCreate[savedInstanceState]; setContentView[R.layout.activity_main]; new DownloadTask[].execute[URL]; } static class DownloadTask extends AsyncTask { @Override protected Void doInBackground[String.. strings] { Document document = null; try { document = [Document]Jsoup.connect[strings[0]].get[]; } catch [IOException e] { e.printStackTrace[]; } return null; } } }Vì việc lấy dữ liệu cần sử dụng mạng nên phải cấp quyền sử dụng internet trong tệp AndroidManifest. xml
Jsoup.parse[String html]0Tiến hành xử lý tài liệu trong phương thức
Jsoup.parseBodyFragment[String html]1, vào trang chủ https. //tập huấn. stdio. vn/huấn luyện cấu trúc HTML khảo sátBật công cụ kiểm tra phần tử [inpsector] trên trình duyệt web, nhận thấy các khóa học đều có điểm chung nằm trong thẻ
Jsoup.parseBodyFragment[String html]2 cóJsoup.parseBodyFragment[String html]3 làJsoup.parseBodyFragment[String html]4Jsoup.parse[String html]1Rút mã để lấy tất cả những thẻ
Jsoup.parseBodyFragment[String html]2 cóJsoup.parseBodyFragment[String html]3 làJsoup.parseBodyFragment[String html]4.Jsoup.parse[String html]2Sau đó, lần duyệt qua tất cả các phần tử trong
Jsoup.parseBodyFragment[String html]8 để lấy các thông tin cần thiết. Mở thẻJsoup.parseBodyFragment[String html]2 cóJsoup.parseBodyFragment[String html]3 làJsoup.parseBodyFragment[String html]4, thấy nội dung như dưới đâyChủ Đề