Jsoup 的認識和簡單使用


  之前做學校軟件協會APP的時候,由於自己不會在服務端寫接口,所以服務端一直是由另一位Z同學完成的,但是突然Z同學被老師調到瀘州幫以前的學長做一個月的臨時web開發去了,所以協會APP的接口只做了一部分就沒了。我也很是無奈啊,想自己邊學邊做,但是時間不允許,馬上就要做畢業設計了,而且還要幫老師寫教材。但自己的需求其實還算比較簡單,只需要在已做好的網站上獲取信息即可,而且之前就知道有網絡爬蟲這種東西(雖然自己沒實現過),所以我想在網上找一找相關的資料,於是便在網上找到了一款HTML解析器,也就是jsoup 。

  在網上找到了Jsoup的中文開發教程:http://www.open-open.com/jsoup/

  多說無益,還是找個實例吧。

  以我校的軟件協會為例,獲取一篇軟件協會的文章,地址為:http://10.10.9.100:2014/NewsDetailInfo.aspx?newsid=615a1431-7766-407e-af62-d372b6cbc54e

  (外網地址為http://www.topcsa.org:2014/NewsDetailInfo.aspx?newsid=615a1431-7766-407e-af62-d372b6cbc54e)

首先,下載並導入jar包。

下載地址:http://jsoup.org/download

倒入包后如下圖所示:

因為我們需要網絡請求,所以添加網絡權限:<uses-permission android:name="android.permission.INTERNET"></uses-permission>。

從瀏覽器上查看我們需要的內容:

布局文件如下:

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

 

下面貼上獲取數據的代碼:

package com.topcsa.zhj_jsoup;

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.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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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();
        }
    }

    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;
        }

        
        @Override
        protected String doInBackground(Integer... params) {
            String str = null;
            String content = "http://10.10.9.100:2014";
            Document doc = null;

            try {
                // 獲取文檔
                doc = Jsoup
                        .connect(
                                "http://10.10.9.100:2014/NewsDetailInfo.aspx?newsid=615a1431-7766-407e-af62-d372b6cbc54e")
                        .timeout(5000).get();
                //獲取<div id="right">對應的內容
                Elements ListDiv = doc.getElementsByAttributeValue("id",
                        "right");
                //查找圖片
                for (Element element : ListDiv) {
                    str = element.html();
                    Elements Listimg = ListDiv.select("img");
                    String strimg;
                    //打印出圖片鏈接
                    for (Element e : Listimg) {
                        strimg = content + e.attr("src");
                        Log.d("Listimg.src", strimg);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return str.replace("/UpFileimage",
                    "http://10.10.9.100:2014/UpFileimage");//將獲取的相對路徑轉換成絕對路徑

        }

        /**
         * 這里的String參數對應AsyncTask中的第三個參數(也就是接收doInBackground的返回值)
         * 在doInBackground方法執行結束之后在運行,並且運行在UI線程當中 可以對UI空間進行設置
         */
        @Override
        protected void onPostExecute(String result) {
            webView.loadData(result, "text/html;charset=utf-8", null);
            textView.setText(result);
        }

    }
}

 運行結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM