android使用Jsoup 抓取頁面的數(shù)據(jù)
jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內(nèi)容。它提供了一套非常省力的API,可通過DOM,CSS以及類似于jQuery的操作方法來取出和操作數(shù)據(jù)。
Jsoup的官方中文地址:http://www.open-open.com/jsoup/parse-document-from-string.htm
在這個網(wǎng)站上你可以找到一些說明,.jar文件的下載,doc文檔的說明等等
jsoup的主要功能如下:
- 從一個URL,文件或字符串中解析HTML;
- 使用DOM或CSS選擇器來查找、取出數(shù)據(jù);
- 可操作HTML元素、屬性、文本;
jsoup是基于MIT協(xié)議發(fā)布的,可放心使用于商業(yè)項目。
Jsoup類下面的方法都是靜態(tài)可直接調(diào)用。幾個方法的說明
Connect()方法,獲得一個Connection,然后調(diào)用Connection對象get()方法獲得Document對象。然后再解析Document對象 Connection提供了一些設置方法timeout(),url()等等
這里貼一下我用到的 Java工程的測試代碼
package com.javen.Jsoup; 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 JsoupTest { static String url="http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html"; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub BolgBody(); //test(); //Blog(); /* * Document doc = Jsoup.connect("http://www.dbjr.com.cn/") * .data("query", "Java") // 請求參數(shù) .userAgent("I ' m jsoup") // 設置 * User-Agent .cookie("auth", "token") // 設置 cookie .timeout(3000) // * 設置連接超時時間 .post(); */// 使用 POST 方法訪問 URL /* * // 從文件中加載 HTML 文檔 File input = new File("D:/test.html"); Document doc * = Jsoup.parse(input,"UTF-8","http://www.dbjr.com.cn/"); */ } /** * 獲取指定HTML 文檔指定的body * @throws IOException */ private static void BolgBody() throws IOException { // 直接從字符串中輸入 HTML 文檔 String html = "<html><head><title> 開源中國社區(qū) </title></head>" + "<body><p> 這里是 jsoup 項目的相關文章 </p></body></html>"; Document doc = Jsoup.parse(html); System.out.println(doc.body()); // 從 URL 直接加載 HTML 文檔 Document doc2 = Jsoup.connect(url).get(); String title = doc2.body().toString(); System.out.println(title); } /** * 獲取博客上的文章標題和鏈接 */ public static void article() { Document doc; try { doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postTitle"); for (Element element :ListDiv) { Elements links = element.getElementsByTag("a"); for (Element link : links) { String linkHref = link.attr("href"); String linkText = link.text().trim(); System.out.println(linkHref); System.out.println(linkText); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 獲取指定博客文章的內(nèi)容 */ public static void Blog() { Document doc; try { doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postBody"); for (Element element :ListDiv) { System.out.println(element.html()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
下面來介紹android中使用Jsoup異步解析網(wǎng)頁的數(shù)據(jù) 請注意: 這里很容易遇到一個亂碼的問題
配置文件:AndroidManifest.xml中加 權(quán)限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
layout的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="200dp" /> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </ScrollView> </LinearLayout>
主要異步加載數(shù)據(jù)的代碼
package com.javen.aaa; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.webkit.WebView; import android.widget.TextView; public class MainActivity extends Activity { private WebView webView; private TextView textView; private static final int DIALOG_KEY = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webView = (WebView) findViewById(R.id.webView); textView=(TextView) findViewById(R.id.textView); try { ProgressAsyncTask asyncTask=new ProgressAsyncTask(webView,textView); asyncTask.execute(10000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String test() { StringBuffer buffer=new StringBuffer(); Document doc; try { doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postTitle"); for (Element element :ListDiv) { Elements links = element.getElementsByTag("a"); for (Element link : links) { String linkHref = link.attr("href"); String linkText = link.text().trim(); buffer.append("linkHref=="+linkHref); buffer.append("linkText=="+linkText); System.out.println(linkHref); System.out.println(linkText); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return buffer.toString(); } // 彈出"查看"對話框 @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_KEY: { ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("獲取數(shù)據(jù)中 請稍候..."); dialog.setIndeterminate(true); dialog.setCancelable(true); return dialog; } } return null; } public static String readHtml(String myurl) { StringBuffer sb = new StringBuffer(""); URL url; try { url = new URL(myurl); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "gbk")); String s = ""; while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } class ProgressAsyncTask extends AsyncTask<Integer, Integer, String> { private WebView webView; private TextView textView; public ProgressAsyncTask(WebView webView,TextView textView) { super(); this.webView=webView; this.textView=textView; } /** * 這里的Integer參數(shù)對應AsyncTask中的第一個參數(shù) 這里的String返回值對應AsyncTask的第三個參數(shù) * 該方法并不運行在UI線程當中,主要用于異步操作,所有在該方法中不能對UI當中的空間進行設置和修改 * 但是可以調(diào)用publish Progress方法觸發(fā)onProgressUpdate對UI進行操作 */ @Override protected String doInBackground(Integer... params) { String str =null; Document doc = null; try { // String url ="http://www.cnblogs.com/zyw-205520/p/3355681.html"; // // doc= Jsoup.parse(new URL(url).openStream(),"utf-8", url); // //doc = Jsoup.parse(readHtml(url)); // //doc=Jsoup.connect(url).get(); // str=doc.body().toString(); doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postBody"); for (Element element :ListDiv) { str=element.html(); System.out.println(element.html()); } Log.d("doInBackground", str.toString()); System.out.println(str); //你可以試試GBK或UTF-8 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return str.toString() ; //return test(); } /** * 這里的String參數(shù)對應AsyncTask中的第三個參數(shù)(也就是接收doInBackground的返回值) * 在doInBackground方法執(zhí)行結(jié)束之后在運行,并且運行在UI線程當中 可以對UI空間進行設置 */ @Override protected void onPostExecute(String result) { webView.loadData(result, "text/html;charset=utf-8", null); textView.setText(result); removeDialog(DIALOG_KEY); } // 該方法運行在UI線程當中,并且運行在UI線程當中 可以對UI空間進行設置 @Override protected void onPreExecute() { showDialog(DIALOG_KEY); } /** * 這里的Intege參數(shù)對應AsyncTask中的第二個參數(shù) * 在doInBackground方法當中,,每次調(diào)用publishProgress方法都會觸發(fā)onProgressUpdate執(zhí)行 * onProgressUpdate是在UI線程中執(zhí)行,所有可以對UI空間進行操作 */ @Override protected void onProgressUpdate(Integer... values) { } } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)實現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細介紹了Android開發(fā)中實現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析
這篇文章主要給大家介紹了關于Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04Android自定義view實現(xiàn)左滑刪除的RecyclerView詳解
RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2022-11-11Android入門之Fragment嵌套Fragment的用法詳解
這篇文章主要為大家詳細介紹了Android中如何實現(xiàn)Fragment嵌套Fragment的相關資料,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-02-02Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法,結(jié)合實例形式分析了Android自動朗讀TTS功能的操作步驟、相關函數(shù)使用方法與注意事項,需要的朋友可以參考下2017-09-09